質問

次のように動作するカスタム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
    }
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top