AspBucket offers ASP.NET, C#, VB, Jquery, CSS, Ajax, SQL tutorials. It is the best place for programmers to learn

Wednesday 11 January 2017

How to get HTML string from Partial view in MVC?

In this article I am going to discuss How to get HTML string from Partial view? Please check below code to get the HTML from partial views.

public string GetViewToString(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();
 }
 }

Pass the View name & object model as a parameter to get HTML string.
 string html_output = GetViewToString("_YourPartialView",model)

0 comments :

Post a Comment

  • Popular Posts
  • Comments