Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the Html Helper, RenderPartial, work? How can I implement a helper that can bring in content from a partial view?

When you use Html.RenderPartial is takes the name of the view you want to render, and renders it's content in that place.

I would like to implement something similar. I would like it to take the name of the view you want to render, along with some other variables, and render the content within a container..

For example:

public static class WindowHelper
{
    public static string Window(this HtmlHelper helper, string name, string viewName)
    {
        var sb = new StringBuilder();

        sb.Append("<div id='" + name + "_Window' class='window'>");
        //Add the contents of the partial view to the string builder.
        sb.Append("</div>");

        return sb.ToString();
    }
}

Anyone know how to do this?

like image 908
Matt Avatar asked Aug 15 '09 22:08

Matt


People also ask

How do I use RenderPartial?

Rendering a Partial View You can render the partial view in the parent view using the HTML helper methods: @html. Partial() , @html. RenderPartial() , and @html. RenderAction() .

What is HTML RenderPartial?

RenderPartial(HtmlHelper, String) Renders the specified partial view by using the specified HTML helper. RenderPartial(HtmlHelper, String, Object) Renders the specified partial view, passing it a copy of the current ViewDataDictionary object, but with the Model property set to the specified model.

How do you call a partial view in Cshtml?

To create a partial view, right-click on view -> shared folder and select Add -> View option. In this way we can add a partial view. It is not mandatory to create a partial view in a shared folder but a partial view is mostly used as a reusable component, it is a good practice to put it in the "shared" folder.

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

Partial function which renders the Partial View. The name of the View and the object of the CustomerModel class are passed to the @Html. Partial function. 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.


1 Answers

The RenderPartial extensions are programmed to render directly to the Response object... you can see this in the source code for them:

....).Render(viewContext, this.ViewContext.HttpContext.Response.Output);

This means that if you change your approach a little bit, you can probably accomplish what you want. Rather than appending everything to a StringBuilder, you could do something like this:

using System.Web.Mvc.Html;

public static class WindowHelper
{
    public static void Window(this HtmlHelper helper, string name, string viewName)
    {
        var response = helper.ViewContext.HttpContext.Response;
        response.Write("<div id='" + name + "_Window' class='window'>");

        //Add the contents of the partial view to the string builder.
        helper.RenderPartial(viewName);

        response.Write("</div>");
    }
}

Note that including System.Web.Mvc.Html allows you access to the RenderPartial() methods.

like image 92
womp Avatar answered Sep 29 '22 01:09

womp