Charu
Charu

Reputation: 2749

What is the use of Html.BeginForm in MVC3

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.

Upvotes: 20

Views: 34678

Answers (5)

John
John

Reputation: 7049

Excellent question, I wondered about that myself quite a bit.

I could be wrong, but here's my guess (a better man may correct me):

In the early days of MVC, there was no razor.

Switching between C#/VB and html was hard syntax-wise, so there was a push towards minimizing those borders.

When creating forms, it was seen beneficial to create the entire form purely on the C#/VB side, without any intermingling manual html, to just have one border to the surrounding world of html.

And since programmers often copy the style of doing things from somewhere else, the practice of using those helpers has persisted even though their benefit has disappeared with the advent of razor.

Upvotes: 0

Jesse Hallam
Jesse Hallam

Reputation: 6964

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. It's just a bit of syntactic sugar over:

<form method="post" action="@Url.Action(...)">

In Microsoft's words:

The ASP.NET MVC framework includes helper methods that provide an easy way to render HTML in a view.

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.BeginForm just gives you a consistent way to start and finish your form:

@using(Html.BeginForm())
{
    @Html.LabelFor(...)
    @Html.EditorFor(...)
}

Html.BeginForm returns an IDisposable object, allowing you to wrap it in the C# using statement. When the using exits, the disposal will call Html.EndForm() automatically for you. Since Html.EndForm returns void it is slightly inconvenient to call from Razor:

@Html.BeginForm()
<formStuff>
@{Html.EndForm();}

A simple @Html.EndForm() will turn in to Write(Html.EndForm()) -> Write(void), ie compile time error.

Upvotes: 35

Dmitry Efimenko
Dmitry Efimenko

Reputation: 11203

What's the purpose of having helpers at all? (Rhetorical question) You could type all inputs manually as well, but helpers... well, help you sometimes.

all it does it put tag. Yes, you can do it manually, or you can be a bit more fancy and do this:

@using(Html.BeginForm())
{
}

This will close the form tag for you as well. So if you keep all your inputs inside the curvy brackets, you don't need to worry about remembering closing the form tag.

Upvotes: 2

darkey
darkey

Reputation: 3722

does something which cannot be done with a simple form

At the end every helper method call is converted to pure HTML so there is nothing that Html.BeginForm can do that can't be done by using the <form> tag directly.

Html.BeginForm is purely an helper method.

Upvotes: 4

Lucero
Lucero

Reputation: 60190

Of course you can code it by hand, but it does have several advantages, such as:

  • it returns an object that is disposable, so that you can put it in a using clause and it will close the form tag for you
  • it computes the URL for the form action

Upvotes: 6

Related Questions