I want to pass a complex object from a Controller Method back to my Ajax call. As far as I know from several researches JSON is the thing of choice at this point.
Here's my Controller Method:
public JsonResult GetUserByData(string fn, string ln, string dep, string cc, string tel, string mail) {
IList<Models.Person> userCollection = Models.Person.GetPersonsByData(fn, ln, dep, tel, cc, mail).ToList();
if (userCollection.Count > 1) {
return Json(new { Complex= true, HTML = PartialView("SomeView.cshtml", userCollection) });
}
else {
return Json(new { Complex = false, HTML = PartialView("SomeOtherView.cshtml", userCollection.First()) });
}
Here's my Ajax call:
$.ajax({
url: 'Home/GetUserByData',
type: 'POST',
dataType: 'html',
data: {
fn: firstname,
ln: lastname,
dep: department,
cc: costcenter,
tel: telephone,
mail: mail
},
success: function (data) {
if (data.Complex)
$('#Customer_Person').html(data.HTML);
else
$('#Notification_Area').html(data.HTML);
}
});
Back in my Script it seems that the Properties "Complex" and "HTML" can not be accessed - What am I doing wrong? Is this the best approach passing complex objects or are there other ways doing this?