ASP.NET MVC > Views - Razor Engine

Form based Razor view in ASP.NET MVC

How to create a form based view?


As discussed in the previous point, we first need to create an action method in the Controller for the view and follow the steps to create the View.

Now, write following code to create a form. (Here we are assuming that we have a CreateNew method in the PersonalDetailsController controller

@using (Html.BeginForm())
{
<text>
First name: <input type="text" name="FirstName" />
Last name: <input type="text" name="LastName" />
<input type="submit" value="Submit" />
</text>
}

When this code renders in the browser, it generates following source code.

<form action="/PersonalDetails/CreateNew" method="post"> 
First name: <input type="text" name="FirstName" />
Last name: <input type="text" name="LastName" />
<input type="submit" value="Submit" />
</form>

Notice that the by default method of the form is post with Html.BeginForm and as we have not passed any parameter to the Html.BeginForm so the action value is by default the current url that means when the form gets submitted after clicking on the Submit button, it gets submitted on /PersonalDetails/CreateNew action method of the PersonalDetailsController.

 Views: 16929 | Post Order: 24



Write for us






Hosting Recommendations