Serilzing MVC行动结果 [英] Serilzing MVC action result

查看:45
本文介绍了Serilzing MVC行动结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用MVC2.0和telerik控件进行UI数据绑定。

我在控制器中的操作是

public ActionResult SearchFilter(RequestModel filterModel)

{

var retList = new List< iadjustment>();

var retList =(List< iadjustment>)GetAllSearchRecords(filter);

返回View(new GridModel(retList));

}


上面代码中的
网格模型期待List< t>。它工作正常。当数据大小很大时,低于错误。

System.InvalidOperationException:使用JSON JavaScriptSerializer进行序列化或反序列化时出错。字符串的长度超过maxJsonLength属性上设置的值





任何人都可以帮我解决这个问题吗?



我尝试了什么:



我尝试使用序列化器

var serializer = new JavaScriptSerializer();

serializer.MaxJsonLength = Int32.MaxValue;

return View(new GridModel(serializer.Serialize(retList)));



但网格模型需要List< t>。

I am using MVC2.0 and telerik control for UI data binding.
My action in controller is
public ActionResult SearchFilter(RequestModel filterModel)
{
var retList = new List<iadjustment>();
var retList = (List<iadjustment>)GetAllSearchRecords(filter);
return View(new GridModel(retList));
}

in above code Grid model expecting List<t>. it works fine. When data size large, getting below error.
System.InvalidOperationException: Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property


Can anyone help me how to resolve this?

What I have tried:

I tried with serializer
var serializer = new JavaScriptSerializer();
serializer.MaxJsonLength = Int32.MaxValue;
return View(new GridModel(serializer.Serialize(retList)));

But Grid model expecting List<t>.

推荐答案

你的问题有点令人困惑。您正在使用哪些代码以及哪些代码因大数据集而失败?



发送列表< t>到View不会将任何内容序列化为JSON。当执行Razor代码生成返回页面的HTML时,它只是传递给Razor视图引擎的对象。



您将使用JSON返回一个对象回到客户端,通常是数据请求,而不是页面请求。



我会告诉你JSON返回字符串的默认允许长度是102,400个字符。你可以通过几种方式改变它。为整个应用程序执行此操作的最简单(和全局)方法是在web.config文件中添加设置:

Your question is a bit confusing. Which code are you really using and which fails with the large data set?

Sending a List<t> to the View doesn't serialize anything to JSON. It's just an object passed to the Razor view engine when the Razor code is executed to generate the HTML for the returned page.

You'd use JSON to return an object back to the client, normally for a data request, not a page request.

I'll tell you that the default allowable length of a JSON return string is 102,400 characters. You can change that in a couple of ways. The easiest (and global) way to do that for your entire application is to add a setting to your web.config file:
<configuration> 
   <system.web.extensions>
       <scripting>
           <webServices>
               <jsonSerialization maxJsonLength="2147483644" />
           </webServices>
       </scripting>
   </system.web.extensions>
</configuration>



另一种方法是设置你创建的序列化程序的MaxJsonLength属性,你说你已经完成了。



现在,如果您仍然收到该错误消息,那是因为您生成的JSON字符串大于MaxJsonLength。在您的示例中,这将是一个大小超过2 GIGABYTES的JSON字符串。将那么多数据发送回客户端是一个可怕的想法。


The other method is to just set the MaxJsonLength property of the serializer you created, which you say you've already done.

Now, if you're still getting that error message, it's because your generated JSON string is greater than MaxJsonLength. In your example, that would be a JSON string greater than 2 GIGABYTES in size. Sending that much data back to the client is a horrible idea.


这篇关于Serilzing MVC行动结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆