gpt4 book ai didi

c# - asp.net MVC4 HtmlHelper 扩展和渲染函数

转载 作者:太空宇宙 更新时间:2023-11-03 15:50:39 25 4
gpt4 key购买 nike

使用这个 stackoverflow 线程,我为我的 HtmlHelper 创建了一个扩展

How can I create a Html Helper like Html.BeginForm

我现在的问题是,我创建了一个带有 Accordion 函数的自定义 .cshtml 文件。没什么特别的,只是一个自定义函数,所以我可以渲染东西。

@helper Accordion(string text, HtmlHelper html)
{
<div class="accordion">
<div><h4>Das ist der Header</h4></div>
<div>
<p>Das ist der Content</p>
<p>@text</p>
@using(){

}

</div>
</div>
}

在这个额外的文件中,您可以使用系统 HtmlHelper(通过 Html),以及您从参数 html 获得的帮助程序。

现在当我尝试使用我的自定义扩展时,即在索引 View 中我可以调用

@using(Html.RoleContainer()){}

但是在这个额外的渲染文件中,我无法调用也看不到 Html.RoleContainerhtml.RoleContainer

如何使 RoleContainer 在额外文件中可用,以便在我的 Accordion 函数中使用它?

最佳答案

查看 Intellisense 后,我意识到 ObjectIndex.cshtml类型是 HtmlHelper<dynamic> .

我所做的是将我的代码更改为

@helper Accordion(string text, System.Web.Mvc.HtmlHelper<dynamic> html)
{
<div class="accordion">
<div><h4>Das ist der Header</h4></div>
<div>
<p>Das ist der Content</p>
<p>@text</p>
@using(html.RoleContainer())
{
for(int i = 0; i < 4; i++){
<li>
@i: Test
</li>
}
}

</div>
</div>
}

这样我就可以使用扩展名 RoleContainer正如您在代码片段中看到的那样。

@ASP.cshtml.PostHelper.Accordion("Content 1", Html)

关于c# - asp.net MVC4 HtmlHelper 扩展和渲染函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25995780/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com