Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Render partial view to string MVC4

I am using the following to render a partial view to 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 the html with strange tags like this below... (I have included a small section as its a big view)

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

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

<div class="modal hide fade" id="dialog" style="display: none;">
like image 974
MrBeanzy Avatar asked Oct 02 '13 17:10

MrBeanzy


People also ask

How do you render a partial view inside a view in MVC?

In order to add Partial View, you will need to Right Click inside the Controller class and click on the Add View option in order to create a View for the Controller.

What is the difference between RenderPartial and partial?

The primary difference between the two methods is that Partial generates the HTML from the View and returns it to the View to be incorporated into the page. RenderPartial, on the other hand, doesn't return anything and, instead, adds its HTML directly to the Response object's output.

Which is the way to render partial view using ASP.NET MVC Razor engine?

RenderPartial function to render Partial View in ASP.Net MVC Razor. The data will be fetched from database using Entity Framework and then the Partial View will be rendered using the @Html. RenderPartial function in ASP.Net MVC Razor.


2 Answers

The following code has always worked for me. Though I can't see any major differences, and can't understand fully why you'd get the output you're getting.

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();
            }
        }
like image 68
Tentux Avatar answered Oct 30 '22 19:10

Tentux


Strange, after a Clean and Rebuild it fixed the issue, must be a VS gremlin.

like image 37
MrBeanzy Avatar answered Oct 30 '22 19:10

MrBeanzy