What is the use of Html.BeginForm in MVC3.
Why do we use it, when we can just add a form tag directly, does this html helper add some capability or does something which cannot be done with a simple form tag.
What is the use of Html.BeginForm in MVC3. Why do we use it, when
Share
The
Html.BeginFormhelper 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. It’s just a bit of syntactic sugar over:In Microsoft’s words:
Of course, no one is making you use them. Its just a matter of preference. In fact, in the early days of MVC, many WebForms developers celebrated their new freedom from server controls a-la
<asp:TextBox>et al., and insisted on writing everything by hand.Using the helpers for your form fields comes highly recommended, since they’re aware of things like form validation.
Html.BeginFormjust gives you a consistent way to start and finish your form:Html.BeginFormreturns anIDisposableobject, allowing you to wrap it in the C#usingstatement. When theusingexits, the disposal will callHtml.EndForm()automatically for you. SinceHtml.EndFormreturnsvoidit is slightly inconvenient to call from Razor:A simple
@Html.EndForm()will turn in toWrite(Html.EndForm()) -> Write(void), ie compile time error.