Skip to content

Instantly share code, notes, and snippets.

@kshyju
Created January 23, 2014 19:16
Show Gist options
  • Save kshyju/8584977 to your computer and use it in GitHub Desktop.
Save kshyju/8584977 to your computer and use it in GitHub Desktop.
renderpartialview
protected string RenderPartialView(string viewName, object model, ViewDataDictionary dictionary = null)
{
if (string.IsNullOrEmpty(viewName))
viewName = this.ControllerContext.RouteData.GetRequiredString("action");
this.ViewData.Model = model;
if (dictionary != null)
{
foreach (var item in dictionary.Keys)
{
this.ViewData.Add(item, dictionary[item]);
}
}
using (var sw = new StringWriter())
{
ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(this.ControllerContext, viewName);
var viewContext = new ViewContext(this.ControllerContext, viewResult.View, this.ViewData, this.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