Skip to content

Instantly share code, notes, and snippets.

@shturm
Created January 18, 2018 09:21
Show Gist options
  • Save shturm/0c53dc495b3aff8148bd6f6d4ba5fd47 to your computer and use it in GitHub Desktop.
Save shturm/0c53dc495b3aff8148bd6f6d4ba5fd47 to your computer and use it in GitHub Desktop.
Render Razor For Email/Page
protected string GetRenderedPartial(string viewName, object model = null)
{
using (var sw = new StringWriter())
{
if (ControllerContext != null)
{
var viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
var viewContext = new ViewContext(ControllerContext, viewResult.View, new ViewDataDictionary(model), TempData, sw);
viewResult.View.Render(viewContext, sw);
}
return sw.ToString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment