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 :)
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.
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.
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With