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

使用htmlhelper.begininform()如何工作?

  •  8
  • Jose  · 技术社区  · 15 年前

    好吧,所以我想知道

    <% using (Html.BeginForm()) { %>
      <input type="text" name="id"/>
    <% } %>
    

    生产

    <form>
      <input type="text" name="id"/>
    </form>
    

    也就是说,如何增加 </form> 最后?我查看了codeplex,但在htmlhelper中没有找到它。有一个endform方法,但是上面怎么知道调用它呢?

    原因是我想创建一个htmlhelper扩展,但不知道如何在使用结束时关闭。

    Any help would be appreciated :)

    2 回复  |  直到 15 年前
        1
  •  16
  •   SLaks    15 年前

    BeginForm 返回一个 IDisposable 哪些调用 EndForm 在里面 Dispose .

    当你写作时 using(Html.BeginForm()) { ... } 编译器生成一个 finally 阻止那个呼叫 处置 ,依次调用 端部形状 然后关闭 <form> 标签。

    您可以通过编写自己的实现 不可分的 .

        2
  •  1
  •   Chad Moran    15 年前

    与slaks所说的非常类似,它生成一个finally块,调用endform,该endform在object.begininform()返回的IDisposable接口上调用Dispose方法。

    BegInform使用rseponse.write将HTML写出到响应中。

    EndForm writes out the closing tag to the Response. Thusly anything that happens in between the constructor returned from BeginForm and the Dispose method will be written to the response properly between the form tags.