Html Helper RenderPartial 如何工作?如何实现一个可以从部分视图引入内容的助手?

发布于 2024-08-02 04:16:57 字数 557 浏览 10 评论 0原文

当您使用 Html.RenderPartial 时,它会获取要渲染的视图的名称,并在该位置渲染其内容。

我想实现类似的东西。我希望它获取您想要渲染的视图的名称以及一些其他变量,并在容器中渲染内容。

例如:

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();
    }
}

有人知道如何做到这一点吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

椒妓 2024-08-09 04:16:57

RenderPartial 扩展被编程为直接渲染到 Response 对象...您可以在它们的源代码中看到这一点:

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

这意味着如果您稍微改变一下方法,您可能可以实现您想要的。 您可以执行以下操作,而不是将所有内容附加到 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>");
    }
}

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

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.

请爱~陌生人 2024-08-09 04:16:57

我们正在 MVC 2 中修复此问题。您将能够调用 Html.Partial() 并以字符串形式获取视图的实际内容。

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.

过去的过去 2024-08-09 04:16:57

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

类似的东西:

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

希望有帮助,

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

Something like:

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

Hope that helps,
Dan

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文