ASP.NET MVC3: Controller extension methods to render a partial view to string


To handle AJAX form requests in my ASP.NET MVC3 controller I needed a way to render a partial view to a string to return a JSON result containing both the (server-side) validation result (as boolean) and a rendered partial view (as string).

Unfortunately in ASP.NET MVC3 there seems no out-of-the-box support for rendering partial views to strings, but I found this create post ASP.NET MVC Render Partial View to String which shows how to implement a RenderPartialViewToString method. To avoid the need of a parent class for each controller that implements this (helper) method I decided to use the following extension methods:

/// 
/// Controller extension class that adds controller methods
/// to render a partial view and return the result as string.
/// 
/// Based on http://craftycodeblog.com/2010/05/15/asp-net-mvc-render-partial-view-to-string/
/// 
public static class ControllerExtension
{

  /// 
  /// Renders a (partial) view to string.
  /// 
  /// Controller to extend
  /// (Partial) view to render
  /// Rendered (partial) view as string
  public static string RenderPartialViewToString(this Controller controller, string viewName)
  {
    return controller.RenderPartialViewToString(viewName, null);
  }

  /// 
  /// Renders a (partial) view to string.
  /// 
  /// Controller to extend
  /// (Partial) view to render
  /// Model
  /// Rendered (partial) view as string
  public static string RenderPartialViewToString(this Controller controller, string viewName, object model)
  {
    if (string.IsNullOrEmpty(viewName))
      viewName = controller.ControllerContext.RouteData.GetRequiredString("action");

      controller.ViewData.Model = model;

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

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

}

Using this extension methods my controller actions handling the AJAX requests look like this:

[HttpPost]
public ActionResult Update(int id, Model model)
{
  if (ModelState.IsValid)
  {
    // Update the database
    [...]

    // Partial/FormUpdated contains a success message
    return Json(new Object[] { true, this.RenderPartialViewToString("Partial/FormUpdated", model) });
  }

  // Partial/Form contains the form with all server-side validation errors
  return Json(new Object[] { false, this.RenderPartialViewToString("Partial/Form", model) });
}
,

One response to “ASP.NET MVC3: Controller extension methods to render a partial view to string”

  1. Thank you very much Jan, this was quite useful to me. I need to generate HTML emails and save them in database, where they are scheduled to be emailed at a later time. So now I can use the same partial view that is used for previewing of the email.

    Great job!

Leave a Reply

Your email address will not be published. Required fields are marked *