What I am trying to do is to get a specific person from the persons list. My Post works fine and I get the person I am looking for as "chosenPerson" in the controller. After that I want to get that person as a complex Json object for use in my view. But something in the serializer.Serialize(chosenPerson) doesn't seem to work and I get an exception on that line saying:
An exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll but was not handled in user code
Additional information: Exception has been thrown by the target of an invocation."
JS in view:
$.ajax({
type: 'POST',
url: '@Url.Action("ReturnPerson", "Home")',
contentType: 'application/json; charset=utf-8',
data: emailUnique,
error: function (event, jqxhr, settings, thrownError) {
console.log(event + " || " + jqxhr + " || " + settings + " || " + thrownError);
}
});
chosenPerson = $.getJSON('/Home/ReturnPerson/');
Controller:
[HttpPost]
public ActionResult ReturnPerson(string emailUnique)
{
var db = new CvAdminContext();
var chosenPerson= db.Persons.Where(p => p.Email == emailUnique);
JavaScriptSerializer serializer = new JavaScriptSerializer();
return Json(serializer.Serialize(chosenPerson), JsonRequestBehavior.AllowGet);
}