Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how does using HtmlHelper.BeginForm() work?

Ok so I want to know how

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

produce

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

namely how does it add the </form> at the end? I looked in codeplex and didn't find it in the htmlhelper. There is a EndForm method, but how does the above know to call it?

The reason is I want to create an htmlhelper extension, but don't know how to close out on the end of a using.

Any help would be appreciated :)

like image 706
Jose Avatar asked Jun 25 '10 20:06

Jose


People also ask

Why we use HTML BeginForm?

The Html. BeginForm helper method contains a couple overloads whose intended purpose is to make writing routed forms easier. It is aware of MVC stucture and makes sure its targeting a controller and action.

What does HtmlHelper class do?

Returns a text input element by using the specified HTML helper, the name of the form field, the value, and the HTML attributes. Returns a text input element by using the specified HTML helper, the name of the form field, the value, and the HTML attributes. Returns a text input element.

What is difference between HTML BeginForm and Ajax BeginForm?

BeginForm() will create a form on the page that submits its values to the server as a synchronous HTTP request, refreshing the entire page in the process. Ajax. BeginForm() creates a form that submits its values using an asynchronous ajax request.


1 Answers

BeginForm returns an IDisposable which calls EndForm in Dispose.

When you write using(Html.BeginForm()) { ... }, the compiler generates a finally block that calls Dispose, which in turn calls EndForm and closes the <form> tag.

You can duplicate this effect by writing your own class that implements IDisposable.

like image 152
SLaks Avatar answered Dec 05 '22 06:12

SLaks