【发布时间】:2010-03-12 21:05:05
【问题描述】:
我想知道是否有可能创建一个功能和行为类似于 Html.BeginForm() 的扩展方法,因为它会生成一个完整的 Html 标记,我可以在 <% { & } %> 标记中指定它的内容。
例如,我可以有这样的视图:
<% using(Html.BeginDiv("divId")) %>
<% { %>
<!-- Form content goes here -->
<% } %>
此功能在我尝试通过this question 中的示例生成的功能的上下文中非常有用
这将使我能够为我将成为的类型创建容器
<% var myType = new MyType(123, 234); %>
<% var tag = new TagBuilder("div"); %>
<% using(Html.BeginDiv<MyType>(myType, tag) %>
<% { %>
<!-- controls used for the configuration of MyType -->
<!-- represented in the context of a HTML element, e.g.: -->
<div class="MyType" prop1="123" prop2="234">
<!-- add a select here -->
<!-- add a radio control here -->
<!-- whatever, it represents elements in the context of their type -->
</div>
<% } %>
我意识到这会产生无效的 XHTML,但我认为可能还有其他好处超过这一点,特别是因为这个项目不需要 XHTML 验证 W3C 标准。
谢谢
戴夫
【问题讨论】:
标签: c# asp.net-mvc xhtml extension-methods html.beginform