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.