内置 HTML 帮助程序的 HTML 帮助程序
我试图用我的头脑去思考一些事情;也许这里有人可以指出我正确的方向。
目前,有一个窗体,上面有一些控件,如下所示:
@Html.ListBoxFor(s => s.SomeListSelection1, new MultiSelectList(Model.SomeList1, "Id", "Text"))
<br />
@Html.ListBoxFor(s => s.SomeListSelection2, new MultiSelectList(Model.SomeList2, "Id", "Text"))
<br />
@Html.ListBoxFor(s => s.SomeListSelection3, new MultiSelectList(Model.SomeList3, "Id", "Text"))
<br />
此窗体出现在许多地方,但可用控件略有不同。
“轻微变化”使我不仅仅想将其作为部分视图来执行,因为我需要将控件复制到许多略有变化的部分中,或者将变化逻辑编码到视图中。
所以我想,“我知道,我会使用很酷的 HTML 助手!!”
但是,所有在线 HTML 帮助程序示例都是非常简单的 html 内容。所以问题是如何在 HTML 帮助器中使用其他 HTML 帮助器?或者,如果这是一个错误的问题,是否有另一种更清晰的替代方案,可以将其作为带有一堆条件渲染逻辑的部分视图?
编辑: 虽然答案确实在技术上回答了我的问题,但我在利用博客条目来完成我想做的事情时遇到了困难。我在这里找到了一个更好的例子(无论如何对我来说): ASP.NET MVC 3 自定义 HTML 帮助程序 - 最佳实践/用途
I'm trying to wrap my head around something; maybe someone here can point me in the right direction.
Currently a have a form that has some controls on it like this:
@Html.ListBoxFor(s => s.SomeListSelection1, new MultiSelectList(Model.SomeList1, "Id", "Text"))
<br />
@Html.ListBoxFor(s => s.SomeListSelection2, new MultiSelectList(Model.SomeList2, "Id", "Text"))
<br />
@Html.ListBoxFor(s => s.SomeListSelection3, new MultiSelectList(Model.SomeList3, "Id", "Text"))
<br />
This form appears in many places with some slight variation on which controls are available.
The "slight variation" makes me not just want to do this as a partial view as I will either need to replicate the controls into many slightly varied partials or code the variation logic into the view.
So I think, "I know, I'll use the cool HTML Helper thingy!!"
But, all the HTML helper samples online are very simple html content. So the question is how can I use other HTML helpers inside an HTML helper? Or, if that is the wrong question is there another cleaner alternative to just making this a partial view with a bunch of conditional rendering logic?
Edit:
While the answer did technically answer my question, I had trouble making use of the blog entries for what I was trying to do. I found a better example (for me anyway) here:
ASP.NET MVC 3 Custom HTML Helpers- Best Practices/Uses
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,你可以。
帮助程序可以包含任意 Razor 标记。
事实上,辅助方法的直接内容是代码块,而不是标记。
有关助手如何工作的更深入讨论,请参阅 我的博客。
Yes, you can.
Helpers can contain arbitrary Razor markup.
In fact, the direct contents of a helper method is a code block, not markup.
For a more in-depth discussion about how helpers work, see my blog.