Html Helper RenderPartial 如何工作?如何实现一个可以从部分视图引入内容的助手?
当您使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
RenderPartial 扩展被编程为直接渲染到 Response 对象...您可以在它们的源代码中看到这一点:
这意味着如果您稍微改变一下方法,您可能可以实现您想要的。 您可以执行以下操作,而不是将所有内容附加到 StringBuilder:
请注意,包含 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:
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:
Note that including System.Web.Mvc.Html allows you access to the RenderPartial() methods.
我们正在 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.
为什么不创建第二个视图并在其中包含部分视图,将名称作为 ViewData 或模型等传递。
类似的东西:
希望有帮助,
担
Why not create a second view and have the partial inside that, pass Name as ViewData or in model etc..
Something like:
Hope that helps,
Dan