I am trying to figure out the best way to send a complex object from my Controller Method to an Ajax call. From my research, it seems that using JSON is the preferred method.
This is what my Controller Method looks like:
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()) });
}
And here is 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);
}
});
However, I seem to be having trouble accessing the "Complex" and "HTML" properties in my script. Am I missing something? Is using JSON the best way to pass complex objects, or are there other methods I should consider?