I have a simple method in my MVC controller:
[HttpPost]
public JsonResult GetAreasForCompany(int companyId)
{
var areas = context.Areas.Where(x => x.Co
Return List Object as Json (Also useful for JqueryUI and Linq Method)
public ActionResult GetItemList()
{
var search = Request.Params["term"];
var itemList = (from items in db.TblItems where items.ItemName.StartsWith(search) select new { label = items.ItemName, value = items.ItemName }).ToList();
return Json(itemList, JsonRequestBehavior.AllowGet);
}
Since class Area
contains Company
and Company
contains collection of Area
you likely have circular references in your object hierarchy which is not supported by the JSON serializer. To solve this, return anonymous objects with only those properties you need, for example
[HttpPost]
public JsonResult GetAreasForCompany(int companyId)
{
var areas = context.Areas
.Where(x => x.Company.CompanyId == companyId)
.Select(a => new
{
AreaId = a.AreaId,
Title = a.Title
});
return Json(areas);
}