I have a simple method in my MVC controller:
[HttpPost]
public JsonResult GetAreasForCompany(int companyId)
{
var areas = context.Areas.Where(x => x.Company.CompanyId == companyId).ToList();
return Json(areas);
}
This is an area object:
public class Area
{
public int AreaId { get; set; }
[Required]
public string Title { get; set; }
public bool Archive { get; set; }
public virtual Company Company { get; set; }
}
And this is how I call the method from the view:
$.ajax({
url: '@Url.Action("GetAreasForCompany")',
type: 'POST',
async: false,
data: "{'companyId': " + companyId + "}",
dataType: 'json',
contentType: 'application/json; charset=utf-8',
error: function () {
alert("Server access failure!");
},
success: function (result) {
response = result;
}
});
I have checked the method in the controller and a list of Area objects gets created. Would you have any idea why do I get the 500 internal server error when the method is called from the view? When I return anything else (like a Dictionary object) everything works fine, it's just when I aim to convert the List of Areas into Json I get an error.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…