My goal is to retrieve a specific person from a list of persons. The POST method works correctly and I am able to obtain the desired person as "chosenPerson" in the controller. However, when I try to convert this person into a complex JSON object using serializer.Serialize(chosenPerson), an exception occurs with the following message:
An exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll but was not handled in user code
Further details: Exception has been thrown by the target of an invocation."
Here is the JavaScript code within the 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/');
As for the 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);
}