代码之家  ›  专栏  ›  技术社区  ›  ETFairfax

html.begininform()扩展类型

  •  6
  • ETFairfax  · 技术社区  · 15 年前

    是否有人知道创建自定义HTMLHelperExtension方法的语法,该方法的行为类似..

    <% using (Html.BeginForm()) {%>
    
    <p>Loads of html stuff here </p>
    
    <% } %>
    

    我在想……的事情。

    有什么想法吗?

    干杯,

    埃特费尔法克斯

    2 回复  |  直到 12 年前
        1
  •  8
  •   Mehrdad Afshari    15 年前

    您需要创建一个实现 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...
        }
    }
    
        2
  •  1
  •   Greg Beech    15 年前

    你需要有这样的方法:

    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
        }
    }