Html Helper,RenderPartial如何工作?如何实现可以从局部视图中引入内容的帮助程序?

时间:2021-07-23 09:02:35

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

当您使用Html.RenderPartial时,将获取您要呈现的视图的名称,并在该位置呈现它的内容。

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?

有人知道怎么做吗?

3 个解决方案

#1


8  

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

RenderPartial扩展被编程为直接呈现给Response对象...您可以在源代码中看到它们:

....).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:

这意味着如果你稍微改变你的方法,你可以完成你想要的。您可以执行以下操作,而不是将所有内容附加到StringBuilder:

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.

请注意,包括System.Web.Mvc.Html允许您访问RenderPartial()方法。

#2


8  

We are fixing this in MVC 2. You will be able to call Html.Partial() and get the actual contents of the view as a string.

我们正在MVC 2中解决这个问题。您将能够调用Html.Partial()并将视图的实际内容作为字符串获取。

#3


0  

Why not create a second view and have the partial inside that, pass Name as ViewData or in model etc..

为什么不创建第二个视图并在其中包含部分内容,将Name作为ViewData或模型等传递。

Something like:

就像是:

<div id='<%= ViewData["Name"] + "_Window"%>' class='window'>
   <% Html.RenderPartial(ViewData["Name"]); %>
</div>

Hope that helps, Dan

希望有所帮助,丹

#1


8  

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

RenderPartial扩展被编程为直接呈现给Response对象...您可以在源代码中看到它们:

....).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:

这意味着如果你稍微改变你的方法,你可以完成你想要的。您可以执行以下操作,而不是将所有内容附加到StringBuilder:

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.

请注意,包括System.Web.Mvc.Html允许您访问RenderPartial()方法。

#2


8  

We are fixing this in MVC 2. You will be able to call Html.Partial() and get the actual contents of the view as a string.

我们正在MVC 2中解决这个问题。您将能够调用Html.Partial()并将视图的实际内容作为字符串获取。

#3


0  

Why not create a second view and have the partial inside that, pass Name as ViewData or in model etc..

为什么不创建第二个视图并在其中包含部分内容,将Name作为ViewData或模型等传递。

Something like:

就像是:

<div id='<%= ViewData["Name"] + "_Window"%>' class='window'>
   <% Html.RenderPartial(ViewData["Name"]); %>
</div>

Hope that helps, Dan

希望有所帮助,丹