Created
October 17, 2013 03:12
-
-
Save traktraktrugui/7018708 to your computer and use it in GitHub Desktop.
Render MVC PartialView To 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
public string RenderPartialViewToString(string viewName, object model) | |
{ | |
if (string.IsNullOrEmpty(viewName)) | |
viewName = ControllerContext.RouteData.GetRequiredString("action"); | |
ViewData.Model = model; | |
using (StringWriter sw = new StringWriter()) | |
{ | |
ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName); | |
ViewContext viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw); | |
viewResult.View.Render(viewContext, sw); | |
return sw.GetStringBuilder().ToString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment