Html.BeginForm() тип расширения
Кто-нибудь знает синтаксис для создания собственного метода HtmlHelperextension, который ведет себя как...
<% using (Html.BeginForm()) {%>
<p>Loads of html stuff here </p>
<% } %>
Я думаю о чем-то вроде...
Есть идеи?
Ура,
ETFairfax
2 ответа
Решение
Вам нужно создать класс, который реализует 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
}
}