Skip to content

Instantly share code, notes, and snippets.

@lazarofl
Created March 28, 2013 12:28
Show Gist options
  • Save lazarofl/5262744 to your computer and use it in GitHub Desktop.
Save lazarofl/5262744 to your computer and use it in GitHub Desktop.
Render a partial view to a variable in ASP.NET MVC
using (StringWriter sw = new StringWriter())
{
ViewData.Model = oCalendarEvent;
ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, "_PARTIALVIEWNAME");
ViewContext viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw);
viewResult.View.Render(viewContext, sw);
eventView = sw.GetStringBuilder().ToString().RemoveAll("\r\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment