0

I am getting the error "Error during serialization or deserialization using the JSON JavaScriptSerializer. The length of the string exceeds the value set on the maxJsonLength property." when trying to convert a MVC partial view to string when the payload exceeds the MaxJsonLength property.

How do I go about setting the MaxJsonLength property in this case? I've tried setting the <jsonSerialization maxJsonLength="2147483644"/> property in the web.config, as per this post, but that doesn't have any effect. I am not entirely where to go from here and looking for a bit of a guidance.

The code errors on the viewResult.View.Render(viewContext, sw); line below:

    protected string ConvertViewToString(string viewName, object model)
    {
        string razorView = string.Empty;

        if (string.IsNullOrWhiteSpace(viewName)) return razorView;

        if (model != null && ViewData != null)
        {
            ViewData.Model = model;
        }

        if (ControllerContext != null)
        {
            ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
            if (viewResult != null)
            {
                using (StringWriter sw = new StringWriter())
                {
                    ViewContext viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);

                    viewResult.View.Render(viewContext, sw);
                    viewResult.ViewEngine.ReleaseView(ControllerContext, viewResult.View);

                    razorView = sw.GetStringBuilder().ToString();
                }
            }
        }

        return razorView;
    }
Community
  • 1
  • 1
Mark Erasmus
  • 2,305
  • 6
  • 25
  • 37
  • Are you sure that is happening when you Render the view and not when you are returning the payload to the client? – Ross Bush Jan 26 '16 at 23:16
  • Yes, it throws an exception on `viewResult.View.Render(viewContext, sw);` line. I have an override of the Json method in the base controller that, but that code isn't reached. – Mark Erasmus Jan 27 '16 at 14:37

1 Answers1

0

The underlying cause of this was due to trying to load too large a result set into the Telerik Kendo grid within the partial view - the Kendo grid was throwing the exeception which is why it was being caught on the viewResult.View.Render(viewContext, sw) line. The solution was to break the result set down into smaller manageable chunks.

Mark Erasmus
  • 2,305
  • 6
  • 25
  • 37