This code snippet returns a partial view as a string, useful, when you want to return part of a view in an Ajax response.
public string RenderViewToString(string viewName, ViewDataDictionary ViewData)
{
using (var sw = new StringWriter())
{
var viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName);
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();
}
}
No comments:
Post a Comment