开发者

Html.BeginForm() type of extension

Does anyone know the syntax for creating a custom HtmlHelperextension method which behaves like..

<% using (Html.BeginForm()) {%>

<p>Loads of html stuff here </p>

<% } %>

I'm thinking of something along the lines of....

Any idea开发者_JS百科s?

Cheers,

ETFairfax


You need to create a class that implements IDisposable interface and return that from your 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...
    }
}


You'd need to have a method something like this:

public static IDisposable BeginTable(this HtmlHelper html, ...)
{
    // write the start of the table here

    return new EndTableWriter();
}

Where the EndTableWriter is something like this:

private class EndTableWriter : IDisposable
{
    public void Dispose()
    {
        // write the end of the table here
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜