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.
Pass the View name & object model as a parameter to get HTML string.
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