将@RenderSection添加到html帮助器

时间:2012-12-13 10:48:50

标签: c# asp.net-mvc html-helper

我需要将代码添加到我的html帮助器中以生成以下html的等效内容:

<div id="buttonrow">@RenderSection("ButtonRow", false)</div>

这可能吗?

这不起作用......

public static MvcHtmlString ButtonRow(this HtmlHelper helper)
{  
TagBuilder buttonRow = new TagBuilder("div");
buttonRow.GenerateId("buttonRow");
buttonRow.InnerHtml = "@RenderSection('ButtonRow', false)";

return MvcHtmlString.Create(buttonRow.ToString(TagRenderMode.Normal));
}

2 个答案:

答案 0 :(得分:1)

@RenderSection是服务器生成的代码片段。也就是说,当Razor引擎呈现视图时,它会将@和其他特殊的Razor标记内容视为要解析的代码片段。

当你写

之类的东西时
buttonRow.InnerHtml = "@RenderSection('ButtonRow', false)";

你只需要将原始字符串写入HTML,即Razor不会解析的字符串。

答案 1 :(得分:0)

您在布局页面外渲染一个部分,如下所示:

exit <- true
相关问题