如何将db对象作为复杂的Json对象返回?

时间:2021-06-17 07:21:04

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:

我想要做的是从人员名单中找到一个特定的人。我的帖子工作正常,我在控制器中找到了我正在寻找的“selectedPerson”。之后,我想让那个人成为一个复杂的Json对象,以便在我的视图中使用。但是serializer.Serialize(selectedPerson)中的某些东西似乎不起作用,我在该行上得到一个例外:

An exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll but was not handled in user code

mscorlib.dll中出现“System.Reflection.TargetInvocationException”类型的异常,但未在用户代码中处理

Additional information: Exception has been thrown by the target of an invocation."

附加信息:调用目标引发了异常。“

JS in view:

JS在视图中:

$.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);
}

1 个解决方案

#1


0  

You have a few issues with code. I would advise using Newtonsoft.Json to serialize the Json. Note I have removed the [HttpPost]. Change your controller to:

你有一些代码问题。我建议使用Newtonsoft.Json来序列化Json。注意我删除了[HttpPost]。将您的控制器更改为:

public JsonResult ReturnPerson(string emailUnique)
{
    var db = new CvAdminContext();
    var chosenPerson= db.Persons.Where(p => p.Email == emailUnique).ToList();

    return Json(JsonConvert.SerializeObject(chosenPerson), JsonRequestBehavior.AllowGet);

}

Then from your client side you can use:

然后从您的客户端,您可以使用:

var email = "whatever@whatever.com";
// you could also use var email = @Model.Email; if view is strongly typed.

var chosenPerson = $.getJSON('/Home/ReturnPerson?emailunique=' + email, function(data) {
    console.log(data);
});

#1


0  

You have a few issues with code. I would advise using Newtonsoft.Json to serialize the Json. Note I have removed the [HttpPost]. Change your controller to:

你有一些代码问题。我建议使用Newtonsoft.Json来序列化Json。注意我删除了[HttpPost]。将您的控制器更改为:

public JsonResult ReturnPerson(string emailUnique)
{
    var db = new CvAdminContext();
    var chosenPerson= db.Persons.Where(p => p.Email == emailUnique).ToList();

    return Json(JsonConvert.SerializeObject(chosenPerson), JsonRequestBehavior.AllowGet);

}

Then from your client side you can use:

然后从您的客户端,您可以使用:

var email = "whatever@whatever.com";
// you could also use var email = @Model.Email; if view is strongly typed.

var chosenPerson = $.getJSON('/Home/ReturnPerson?emailunique=' + email, function(data) {
    console.log(data);
});