字符串的长度超过上maxJsonLength属性设置的值。在MVC3 [英] The length of the string exceeds the value set on the maxJsonLength property. in MVC3

查看:2641
本文介绍了字符串的长度超过上maxJsonLength属性设置的值。在MVC3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MVC3(.cshtml文件)

MVC3 (.cshtml File)

$.getJSON(URL, Data, function (data) {

                    document.getElementById('divDisplayMap').innerHTML = data;

                    if (data != null) {
                        $('#myTablesId').show();
                        tdOnclickEvent();

                    }
                    else {
                        $('#myTablesId').hide();
                    }
                }).error(function (xhr, ajaxOptions, thrownError) { debugger; });

在服务器端

 public JsonResult ZoneType_SelectedState(int x_Id, int y_Id)
    {
    JsonResult result = new JsonResult();
     result.Data = "LongString";//Longstring with the length mention below
    return Json(result.Data,"application/json", JsonRequestBehavior.AllowGet);
    }

从服务器端我传递与1194812和超过该长度的字符串。
但我得到的错误说法。

from the server side i am passing the string with the length of 1194812 and more than that. but i am getting the error saying the

"Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property."

请帮我出ASP

推荐答案

您可以写一个自定义的ActionResult,这将允许您指定数据串行器能够处理的最大长度:

You could write a custom ActionResult which will allow you to specify the maximum length of data that the serializer can handle:

public class MyJsonResult : ActionResult
{
    private readonly object data;
    public MyJsonResult(object data)
    {
        this.data = data;
    }

    public override void ExecuteResult(ControllerContext context)
    {
        var response = context.RequestContext.HttpContext.Response;
        response.ContentType = "application/json";
        var serializer = new JavaScriptSerializer();
        // You could set the MaxJsonLength to the desired size - 10MB in this example
        serializer.MaxJsonLength = 10 * 1024 * 1024;
        response.Write(serializer.Serialize(this.data));
    }
}

,然后使用它:

public ActionResult ZoneType_SelectedState(int x_Id, int y_Id)
{
    string data = "LongString";//Longstring with the length mention below;
    return new MyJsonResult(data);
}

这篇关于字符串的长度超过上maxJsonLength属性设置的值。在MVC3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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