1

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);
    }
Community
  • 1
  • 1
Chris
  • 2,953
  • 10
  • 48
  • 118
  • 1
    Possible duplicate of [JavaScriptSerializer.MaxJsonLength exceeded. What's the best practice for handling this?](http://stackoverflow.com/questions/1045984/javascriptserializer-maxjsonlength-exceeded-whats-the-best-practice-for-handli) – Paul Swetz Sep 20 '16 at 17:30
  • The answer you linked (and its linked answers) reference server-side code. Your question only shows us your client-side code. Have you implemented the server-side changes cited in your link? – Jasen Sep 20 '16 at 17:35
  • @Jasen, are you asking me or Paul Swetz? – Chris Sep 20 '16 at 18:02
  • 2
    This is a server side error. You cannot fix a server side error from the client... – Mark Schultheiss Sep 20 '16 at 18:19
  • Some options might be - filter on the server based on your text entry, OR do multiple calls, creating customer list object on the client from the (smaller) blocks on the client (with a delay for the multiple ajax calls to get the blocks of data) (sounding horrid to me already), OR do the server side modification to the size (and deal with the issue when it runs out of the NEW max)...when I did this, I opted to limit to a fixed max count, filtering server side (ranking based on a match algorithm for the max list) – Mark Schultheiss Sep 20 '16 at 18:24
  • @MarkSchultheiss, I was leaning towards one of your suggestions, i was going to do an ajax call on every keyup event, that should chunk the data into reasonable sizes – Chris Sep 20 '16 at 18:26
  • with A DELAY so if I type "Fred" fast I get one call – Mark Schultheiss Sep 20 '16 at 18:27

1 Answers1

1

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.

Kevin B Burns
  • 1,032
  • 9
  • 24