2013-10-01 2 views
0

MVC3 (.cshtml Файл)Длина строки превышает значение, заданное для свойства maxJsonLength. в MVC3

$.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 и более, чем это. , но я получаю ошибку говоря о

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

Пожалуйста, помогите мне ASP

ответ

2

Вы можете написать собственный ActionResult, который позволит вам определить максимальную длину данных, сериализатор может обрабатывать:

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

Попробуйте обновить свой метод Controller, как показано ниже:

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

Надеется, что это помогает ...

Смежные вопросы