Here is my code :
[HttpGet]
public ActionResult GetCategor(int catId)
{
using (uuEntities ems = new uuEntities())
{
return Json(ems.SupportSubCats.Where(x => x.CatID == catId).Select(
x => new
{
SubId = x.SubCatID,
SUbName = x.SubCatName
}).ToList(), JsonRequestBehavior.AllowGet);
}
}
What I have tried :
In controller :
[HttpGet]
public ActionResult GetCategor(int catId)
{
return Json(_service.List(int catId), JsonRequestBehavior.AllowGet);
}
}
In Service :
public void List(int catId)
{
return new GenericRepository<SupportSubCategory>(_factory.ContextFactory)
.Get(filter: (x => x.CatID == catId))
.Select(x => new
{
SubId = x.SubCatID,
SUbName = x.SubCatName
}).ToList();
}
I think my return type is incorrect please suggest me the solution. Near public void, i am getting an error that void can not return the list.
A
void
methods does not return any value to it's caller. You can use an emptyreturn
in avoid
method only to exit the method - but you can't return any value.This code is perfectly valid and widely used as a common practice:
Usually, you use this pattern when you are passing parameters into the method and need to validate them before actually performing the rest of the method.
This code, however, is not valid and will not compile:
Following our conversation in the comments, you should probably create a class to hold the results of the
Select
instead of using an anonymous type, and return a list of that class from your method:...