Chris Chris - 2 months ago 18
Javascript Question

How to set the maxJsonLength property?

I have a JsonResult returning 29833 records, of a CustomerID and a CustomerName. I am trying to load this into an AutoComplete, but I keep getting this error.


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


I did some digging around on the subject and came across this link here

So I read over it and the answer provided didn't work out for me and then the next suggestion looked promising until I looked more at the code and came to the conclusion that it won't work for me because I am using JQuery Ajax to get the JsonResult. Now I am not sure what to do, here is the JQuery that I am using

function LoadCustomers() {
$.ajax({
type: "GET",
url: "/Test/GetAllCustomers",
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (data, textStatus, jqXHR) {
ShowCustomers(data);
}
});
}

function ShowCustomers(custdata) {
$("#acCustomers").kendoAutoComplete({
dataSource: custdata,
filter: "startswith",
placeholder: "Select Customer...",
dataTextField: "CustomerName"
});
}


I even tried just populating a grid but to no avail. Any idea's on how I can get this to work properly going about it the way I am going about it? I think as a last resort I would have to change my stored procedure around and pass in characters on every keyup event, I don't know if that would be a good idea or maybe it is, I don't know. Either way I sure could use some help or direction

EDIT
The reason that this is not a duplicate based on the supplied link is because I am not working server-side, I am working Client-Side.

EDIT 2

Here is my JsonResult

public JsonResult GetAllCustomers(string name)
{
PGDAL dal = new PGDAL();
List<Customer> lst = dal.GetAllCustomers();

return Json(lst, JsonRequestBehavior.AllowGet);
}

Answer

One thing I have learned from some experience is that it seems like ASP.net MVC ignores any JSON Max value you place in the Web.config file. I normally just do the following:

var JsonSerializer = new JavaScriptSerializer();
JsonSerializer.MaxJsonLength = Int32.MaxValue;

As Paul Swetz linked up top, you might find some more resources in managing the MAX value but I am pretty sure this will be the most widely accepted answer.