为自己的助手创建使用?像 Html.BeginForm
我想知道,是否可以使用 using 创建您自己的帮助器定义?例如以下创建表单的内容:
using (Html.BeginForm(params))
{
}
我想制作我自己的助手。所以我想做一个简单的例子
using(Tablehelper.Begintable(id)
{
<th>content etc<th>
}
,它将在我的视图中输出
<table>
<th>content etc<th>
</table>
这可能吗?如果是这样,怎么办?
谢谢
I was wondering, is it possible to create your own helper definition, with a using? such as the following which creates a form:
using (Html.BeginForm(params))
{
}
I'd like to make my own helper like that. So a simple example I'd like to do
using(Tablehelper.Begintable(id)
{
<th>content etc<th>
}
which will output in my view
<table>
<th>content etc<th>
</table>
Is this possible? if so, how?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当然,这是可能的:
然后:
将产生:
我还建议您阅读有关 模板化 Razor 委托。
Sure, it's possible:
and then:
will yield:
I'd also recommend you reading about Templated Razor Delegates.
是的;但是,要使用
Tablehelper.*
,您需要对基本视图进行子类化并添加Tablehelper
属性。不过,可能更容易的是向HtmlHelper
添加一个扩展方法:这将允许您编写:
但这反过来又需要各种其他管道(在
BeginTable< 处启动元素) /code>,并在返回值上的
Dispose()
中结束)。Yes it is; however, to use
Tablehelper.*
you would need to subclass the base-view and add aTablehelper
property. Probably easier, though, is to add an extension method toHtmlHelper
:which will allow you to write:
but this will in turn require various other bits of plumbing (to start the element at
BeginTable
, and end it inDispose()
on the returned value).