0

Okay, so interesting problem...

I am returning a Partial View as a string via this post.

Here's what my base Controller looks like:

public abstract class JsonController : Controller
  {
    protected string RenderPartialViewToString(string viewName, object model)
    {
      if (string.IsNullOrEmpty(viewName))
        viewName = ControllerContext.RouteData.GetRequiredString("action");

      ViewData.Model = model;

      using (var sw = new StringWriter())
      {
        var viewEngineResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
        var viewContext = new ViewContext(ControllerContext, viewEngineResult.View, ViewData, TempData, sw);
        viewEngineResult.View.Render(viewContext, sw);

        return sw.GetStringBuilder().ToString();
      }
    }
  }

And here's how it's used:

ret = Json(RenderPartialViewToString("_ReportData", viewModel));

However, now the result of that view is too big and I am getting the dreaded error:

"The length of the string exceeds the value set on the maxJsonLength property".

I am not sure how to remedy this. Obviously, the setting in the web.config is NOT obeyed by the default JavaScriptSerializer as much documentation states this.

However, trying custom solutions like the LargeJsonResult class simply results in the string getting serialized twice, which is not what I need (and definitely doesn't work). So, I'm at a loss. Certainly there has to be a way to handle this so I can return the partial view as a string and also specify the maxJsonLength... Any suggestions?

And yes, I know the larger issue here is sending back too much data to the client; that issue will get addressed in the future. This is the problem I need solved now. Thanks.

UPDATE - MY FULL SOLUTION

Thanks to Sergey, I got this working. In case anyone else wants to do it this way, here's the steps.

First thing is to go build out the LargeJsonResult class specified here.

I then utilized my base controller class to call RenderPartialViewToString which can be found here.

Next is the code:

var serializer = new JavaScriptSerializer(); serializer.MaxJsonLength = Int32.MaxValue; var viewString = RenderPartialViewToString("_ReportData", viewModel);

  ret = new LargeJsonResult()
          {
            Data = new
                     {
                       result = "Success",
                       html = viewString
                     }
          };

But there was still one adjustment to make, and that was fixing the JQuery callback to fetch out the html part of the return. The callback function originally looked like this:

function GetReportComplete(response, status, xhr) { ... $('#Report').html(response);

But it needed to be adjusted to look like this:

function GetReportComplete(response, status, xhr) { ... $('#Report').html($.parseJSON(response).html);

And with that, it works.

Community
  • 1
  • 1
Chris Holmes
  • 11,444
  • 12
  • 50
  • 64

1 Answers1

1

This worked for me in the same situation:

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

        return serializer.Serialize(...);

UPDATE:

First I use the following code to serialize a partial view:

    protected static String SerializeControl(string controlPath, object model)
    {
        var page = new ViewPage();
        var ctl = (ViewUserControl)page.LoadControl(controlPath);
        page.Controls.Add(ctl);
        page.ViewData.Model = model;
        page.ViewContext = new ViewContext();
        var writer = new StringWriter();
        System.Web.HttpContext.Current.Server.Execute(page, writer, false);
        var outputToReturn = writer.ToString();
        writer.Close();
        return outputToReturn.Trim();
    }

Then I use the code I've written initially to add this string to JSON:

...
    var htmlsb = new StringBuilder();
...
    // listObj initialization here, doesn't matter for question
...
    htmlsb.Append(SerializeControl("~/Views/Shared/ListItem.ascx", listObj));

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

    return serializer.Serialize(new {result = "Success", html = htmlsb.ToString()});
Sergey Kudriavtsev
  • 10,328
  • 4
  • 43
  • 68
  • But where at Sergey? That doesn't make much sense without context. The Partial View is already being converted to a string to be returned as part of a JsonResult. Serializing it a second time puts additional quotes around the result, which doesn't work. – Chris Holmes Oct 05 '11 at 19:36
  • @Chris Will put the code to answer - too bad formatting in comment – Sergey Kudriavtsev Oct 05 '11 at 19:47
  • Thanks Sergey. Your solution got me on the right path. I was able to make it work with the RenderPartialViewToString method, your serializer code and an adjustment to my JQuery callback method. – Chris Holmes Oct 06 '11 at 14:29