简体   繁体   English

在MVC 3中将JSON对象传输到View

[英]Transfer of JSON objects to View in MVC 3

I have such a trouble. 真麻烦 I want to return JSON object from Controller to View after AJAX request. 我想在AJAX请求后将JSON对象从Controller返回到View。 JS code is: JS代码是:

        $.ajax(
         {
                url : '/Order/GetArticleForBasicPosition',
                data : article,
                type : 'POST',

                success : function (data) 
                {
                    alert("yyyyyyy");    
                },
                 error:function (xhr, ajaxOptions, thrownError)
                 {                   
                        alert(xhr.status); 
                        alert(thrownError);
                 }  
         });

And Controller is: 控制器是:

   [HttpPost]
    public JsonResult GetArticleForBasicPosition(string article)
    {
        Article articleInfo = _service.GetArticleForBasicPosition(article);

        return Json(articleInfo);
    }

And I get 500 Internal Server Error. 而且我收到500 Internal Server Error。 I was debugging controller and I see that it gets correct parameter 'article' and service methhod returns correct object. 我正在调试控制器,并且看到它获取了正确的参数“ article”,并且服务方法返回了正确的对象。 I tryed both GET and POST types of request. 我尝试了GET和POST类型的请求。

Actually when I modified my controller as: 实际上,当我将控制器修改为:

  [HttpPost]
    public JsonResult GetArticleForBasicPosition(string article)
    {
        var articleInfo = new Article() {GoodsName = "ffff", GoodsPrice = 1234, CatalogueName = "uuuuuuui"};

        return Json(articleInfo);
    }

everything went ok. 一切顺利。

I suggest that the reason is my object size(I use EntityFramework and articleInfo has a lot of navigation properties), but didn't found anybody who wrote about the same trouble. 我建议原因是我的对象大小(我使用EntityFramework,articleInfo具有很多导航属性),但没有发现有人写过同样的麻烦。

Does anybody know what is the reason of such trouble and if it is size of the object what is the best practice to solve it? 有人知道这种麻烦的原因是什么,如果它是物体的大小,什么是解决它的最佳实践?

Thanks. 谢谢。

I suggest that the reason is my object size(I use EntityFramework and articleInfo has a lot of navigation properties), but didn't found anybody who wrote about the same trouble. 我建议原因是我的对象大小(我使用EntityFramework,articleInfo具有很多导航属性),但没有发现有人写过同样的麻烦。

Ayende wrote blogged it . Ayende写的博客 Many of my answers on this site in the asp.net-mvc tag are about it . 很多我的答案在本网站的asp.net - MVC的标签是

The it is called view models. 就是所谓的视图模型。 You should never pass any domain objects to your views. 您绝对不应将任何域对象传递给视图。 You should design view models specifically tailored to the needs of the view and containing only the necessary properties. 您应该设计专门为视图需求量身定制的视图模型,并且仅包含必要的属性。

I guess the problem comes from the fact that either your domain models contain some recursive structure which obviously cannot be serialized into JSON or at the moment the result is being executed and the serializer tries to touch the model you passed, your data context is long gone and disposed. 我想问题出在以下事实:您的域模型包含一些递归结构,这些递归结构显然无法序列化为JSON,或者在执行结果的那一刻,序列化程序尝试触摸您传递的模型,因此数据上下文早已消失并处置。

So try this: 试试这个:

[HttpPost]
public JsonResult GetArticleForBasicPosition(string article)
{
    Article articleInfo = _service.GetArticleForBasicPosition(article);
    return Json(new
    {
        Property1NeededByTheView = x.Foo,
        Property2NeededByTheView = x.Bar.Baz
    });
}

Also ensure that _service.GetArticleForBasicPosition doesn't throw an exception or you might get a 500 error. 还要确保_service.GetArticleForBasicPosition不会引发异常,否则您可能会收到500错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM