Mat*_*att 6 c# asp.net-mvc html-helper renderpartial partial-views
当您使用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();
}
}
Run Code Online (Sandbox Code Playgroud)
有人知道怎么做吗?
RenderPartial扩展被编程为直接呈现给Response对象...您可以在源代码中看到它们:
....).Render(viewContext, this.ViewContext.HttpContext.Response.Output);
Run Code Online (Sandbox Code Playgroud)
这意味着如果你稍微改变一下你的方法,你可以完成你想要的.您可以执行以下操作,而不是将所有内容附加到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>");
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,包括System.Web.Mvc.Html允许您访问RenderPartial()方法.
| 归档时间: |
|
| 查看次数: |
5443 次 |
| 最近记录: |