Created
May 16, 2013 15:43
-
-
Save corymartin/5592684 to your computer and use it in GitHub Desktop.
Renders a partial template with it's model and returns the output as a string.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Returns the rendered HTML for a partial view. | |
/// </summary> | |
/// <param name="partial">Path to partial template.</param> | |
/// <param name="model"></param> | |
/// <returns></returns> | |
protected string RenderPartialToString(string partial, object model) | |
{ | |
this.ViewData.Model = model; | |
using (var sw = new StringWriter()) | |
{ | |
var viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, partial); | |
var viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw); | |
viewResult.View.Render(viewContext, sw); | |
viewResult.ViewEngine.ReleaseView(ControllerContext, viewResult.View); | |
return sw.GetStringBuilder().ToString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment