.Net MVC Html.BeginForm tutorial with examples

When building web applications using the .NET MVC framework, one of the most common tasks is creating forms to allow users to input data. To make this process easier, the framework provides the HTML.BeginForm method. This method generates the HTML code necessary to create a form and also includes additional features like anti-forgery tokens and validation.

In this blog, we will explore how to use HTML.BeginForm in .NET MVC and the various options available. HTML.BeginForm is a method provided by the HtmlHelper class in .NET MVC. It creates an HTML form element that you can use to collect input from users. Here is an example of how to use it:

Html.BeginForm Example

    @{Html.BeginForm("ActionName", "ControllerName");}
    

Generated HTML Tag

    <form action="/ControllerName/ActionName" method="post">
    
Note: Above Html.BeginForm method will only creates <form> opening tag, you need to manually close this tag. See below example:

 

Close Html.BeginForm manually

@{Html.EndForm();}

Generated HTML Tag

</form>

 

Html.BeginForm Method Auto Close example

In order to auto close Html.BeginForm method we will use @using statement.

@using (Html.BeginForm("ActionName", "ControllerName"))
{
Html.BeginForm Auto Close Example
}

Generated HTML Tag

<form action="/ControllerName/ActionName" method="post">    
<div>Html.BeginForm Auto Close Example</div>
</form>

 

Html.BeginForm with GET method example

In this example we will see how to set FormMethod of Html.BeginForm to GET method.

@using (Html.BeginForm("ActionName", "ControllerName", FormMethod.Get))
{
    
Html.BeginForm with GET method
}

Generated HTML Tag

<form action="/ControllerName/ActionName" method="get">    
<div>Html.BeginForm with GET method</div>
</form>

 

How to set ID in Html.BeginForm method example

In this example we will see how to set Id to Html.BeginForm method.

@using (Html.BeginForm("ActionName", "ControllerName", FormMethod.Post, new { id = "signupform" }))
{
Html.BeginForm with Form Id
}

Generated HTML Tag

<form action="/ControllerName/ActionName" method="get">    
<div>Html.BeginForm with GET method</div>
</form>

 

Set custom properties in Html.BeginForm method example

In this example we will see how to set custom properties in Html.BeginForm method.

@using (Html.BeginForm("ActionName", "ControllerName", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
Html.BeginForm with custom properties
}

Generated HTML Tag

<form action="/ControllerName/ActionName" enctype="multipart/form-data" method="post">   
<div>Html.BeginForm with custom properties </div>
</form>

 

Set Area name in Html.BeginForm method example

In this example we will see how to define Area name in Html.BeginForm method.

@using (Html.BeginForm("ActionName", "ControllerName", FormMethod.Post, new { area = "Admin" }))
{
 
Html.BeginForm with Area Name
}

Generated HTML Tag

<form action="/ControllerName/ActionName" area="Admin" method="post">    
<div>Html.BeginForm with Area Name </div>
</form>

HTML.BeginForm is a powerful tool provided by the .NET MVC framework that makes it easy to create forms in our web applications. By using this method, we can quickly generate HTML code for our forms, specify the form method and action, add anti-forgery tokens, and include validation. This helps to streamline the development process and make our applications more secure and user-friendly.

 
Best quality Asp .Net Ajax Control Toolkit tutorials.

Give your valuable comments.

Name
Email
Comment
6 + 5 =
 

About Us | Terms of Use | Privacy Policy | Disclaimer | Contact Us Copyright © 2012-2024 CodingFusion
50+ C# Programs for beginners to practice