12

I am trying to send json from my MVC controller, its throwing exception, Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property.

I googled and added the max length in my config, also overridden my json method, nothing working out.

Here is my web config and my method, its throwing exception. in appsetting

<add key="aspnet:MaxJsonDeserializerMembers" value="2147483647" />
<system.web.extensions>
    <scripting>
      <webServices>
        <jsonSerialization maxJsonLength="2147483647">
        </jsonSerialization>

    </scripting>
  </system.web.extensions>

over ridden method

 protected override JsonResult Json(object data, string contentType, System.Text.Encoding contentEncoding, JsonRequestBehavior behavior)
        {
            return new JsonResult()
            {
                Data = data,
                ContentType = contentType,
                ContentEncoding = contentEncoding,
                JsonRequestBehavior = behavior,
                MaxJsonLength = Int32.MaxValue
            };
        }

my method

 public JsonResult GetAttributeControls()
        {
            List<SelectListItem> attrControls;

            using (var context = new Context())
            {
                attrControls = context.AttributeControls.ToList().
                    Select(ac => new SelectListItem { Text = ac.Name, Value = ac.AttributeControlId.ToString() }).ToList();
            }
            //var jsonResult = Json(attrControls, JsonRequestBehavior.AllowGet);
            //jsonResult.MaxJsonLength = int.MaxValue;
            //return jsonResult;
            return Json(attrControls,JsonRequestBehavior.AllowGet);
        }

I am getting exception in the below line this is my load.chtml file

<script type="text/javascript">
    var InitialRowData = '@Html.Raw(Json.Encode(Model))';
    var isLayoutEditable = false;
    var occupied = '@Model.occupied';
    var notoccupied = '@Model.notoccupied';
    var blocked = '@Model.blocked';
</script>

@Html.Raw(Json.Encode(Model))';

there json maximum length is around 200000, how to increase the size, nothing working out. any help please?

Thanks in advance.

tereško
  • 58,060
  • 25
  • 98
  • 150
user1845163
  • 307
  • 1
  • 5
  • 17

2 Answers2

32

Okay, so I just recently had the exact same issue. I was trying to do an @Html.Raw(Json.Encode(Model)) to send the model through to javascript and was getting an error that the string was too long.

I scoured around for an answer to this, and could not find anything that answered the question directly, however, I found This Stack Answer that I then used to figure out how to solve our issue.

The idea here is to set up a JavaScriptSerializer, manually set the MaxJsonLength, serialize the model, and then pass it through to javascript.

In the HTML of your view:

@{
    var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
    serializer.MaxJsonLength = Int32.MaxValue;
    var jsonModel = serializer.Serialize(Model);
}

In your javascript:

<script>
    $(function () {
        var viewModel = @Html.Raw(jsonModel);
        // now you can access Model in JSON form from javascript
    });
</script>
Community
  • 1
  • 1
mhodges
  • 10,938
  • 2
  • 28
  • 46
10

I have recently faced the same issue and change my serialize code and its working as expected. Use below code in your code:

@using Newtonsoft.Json;

<div>@Html.Raw(JsonConvert.SerializeObject(Model))</div>
Wouter Vanherck
  • 2,070
  • 3
  • 27
  • 41
user12030403
  • 109
  • 1
  • 2