有人知道创建一个行为类似于..的自定义HtmlHelperextension方法的语法吗?
<% using (Html.BeginForm()) {%>
<p>Loads of html stuff here </p>
<% } %>我在想一些关于……的东西。
有什么想法吗?
干杯,
ETFairfax
发布于 2009-10-30 17:12:37
您需要创建一个实现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...
}
}发布于 2009-10-30 17:12:06
你需要有一个类似这样的方法:
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
}
}https://stackoverflow.com/questions/1648711
复制相似问题