Html.BeginForm()类型的扩展
-
22-07-2019 - |
题
有谁知道语法创建其行为类似于..定制HtmlHelperextension方法
<% using (Html.BeginForm()) {%>
<p>Loads of html stuff here </p>
<% } %>
我一起的......
行思考的东西任何想法?
干杯,
ETFairfax
解决方案
您需要创建一个实现IDisposable
接口的类,并返回,从你HtmlHelper
。
public static class HtmlHelperTableExtensions {
private class TableRenderer : IDisposable {
HtmlHelper html;
public TableRenderer(HtmlHelper html) {
this.html = html;
}
public void Dispose() {
HtmlHelperTableExtensions.EndTable(html);
}
}
public static IDisposable BeginTable(this HtmlHelper html) {
// print begin table here...
return new TableRenderer(html);
}
public static void EndTable(this HtmlHelper html) {
// print end table here...
}
}
其他提示
您就需要有一种方法是这样的:
public static IDisposable BeginTable(this HtmlHelper html, ...)
{
// write the start of the table here
return new EndTableWriter();
}
当EndTableWriter
是这样的:
private class EndTableWriter : IDisposable
{
public void Dispose()
{
// write the end of the table here
}
}
不隶属于 StackOverflow