The length of the string exceeds the value set on the maxJsonLength property. in MVC3

喜你入骨 提交于 2019-12-04 06:32:04

问题


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

on Server Side

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

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."

Please help me out ASP


回答1:


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

and then use it:

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



回答2:


Try to update your Controller method as shown below:

public JsonResult ZoneType_SelectedState(int x_Id, int y_Id)
{
    var result = Json("LongString", JsonRequestBehavior.AllowGet);
    result.MaxJsonLength = int.MaxValue;
    return result;
}

Hope this helps...



来源:https://stackoverflow.com/questions/19108743/the-length-of-the-string-exceeds-the-value-set-on-the-maxjsonlength-property-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!