render,partial,view,as,string,return,html,of,view

Kubilay 7/27/2016 0

this code will let you render partial view and return string of view html

C#
 public string RenderRazorViewToString( string viewName, object model)
 {
      ViewData.Model = model;
      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();
}
 }
 

Report Bug

Please Login to Report Bug

Reported Bugs

Comments

Please Login to Comment

Comments