创建使用了自己的助手?像Html.BeginForm [英] Create using for own helper? like Html.BeginForm
本文介绍了创建使用了自己的助手?像Html.BeginForm的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道,是有可能创造了自己的助手定义,与使用?比如它创建一个表单以下内容:
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?
感谢
推荐答案
当然,这是可能的:
public static class HtmlExtensions
{
private class Table : IDisposable
{
private readonly TextWriter _writer;
public Table(TextWriter writer)
{
_writer = writer;
}
public void Dispose()
{
_writer.Write("</table>");
}
}
public static IDisposable BeginTable(this HtmlHelper html, string id)
{
var writer = html.ViewContext.Writer;
writer.Write(string.Format("<table id=\"{0}\">", id));
return new Table(writer);
}
}
然后:
@using(Html.BeginTable("abc"))
{
@:<th>content etc<th>
}
将产生:
<table id="abc">
<th>content etc<th>
</table>
我也建议你阅读有关模板化剃刀代表。
这篇关于创建使用了自己的助手?像Html.BeginForm的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文