Mark partial view of MVC4 string

I use the following to partially represent a string ...

protected string RenderPartialViewToString(string viewName, object model) { if (string.IsNullOrEmpty(viewName)) viewName = ControllerContext.RouteData.GetRequiredString("action"); ViewData.Model = model; using (var sw = new StringWriter()) { ViewEngineResult viewResult = ViewEngines.Engines.FindPartialView(ControllerContext, viewName); var viewContext = new ViewContext(ControllerContext, viewResult.View, ViewData, TempData, sw); viewResult.View.Render(viewContext, sw); return sw.GetStringBuilder().ToString(); } } 

However, it returns html with strange tags like below ... (I included a small section as my large view)

 <$A$><div</$A$><$B$> class="modal hide fade"</$B$><$C$> id="dialog"</$C$><$D$> 

This happens throughout HTML. This section should look like this ...

 <div class="modal hide fade" id="dialog" style="display: none;"> 
+6
source share
2 answers

Strange, after cleaning and rebuilding, he fixed the problem, there should be VS gremlins.

0
source

The following code always worked for me. Although I do not see any serious differences and cannot fully understand why you will get the result that you get.

 public static String RenderRazorViewToString(ControllerContext controllerContext, String viewName, Object model) { controllerContext.Controller.ViewData.Model = model; using (var sw = new StringWriter()) { var ViewResult = ViewEngines.Engines.FindPartialView(controllerContext, viewName); var ViewContext = new ViewContext(controllerContext, ViewResult.View, controllerContext.Controller.ViewData, controllerContext.Controller.TempData, sw); ViewResult.View.Render(ViewContext, sw); ViewResult.ViewEngine.ReleaseView(controllerContext, ViewResult.View); return sw.GetStringBuilder().ToString(); } } 
+10
source

Source: https://habr.com/ru/post/955121/


All Articles