Free Web Development Online Tutorials, Learn to Code
Submit a form in asp.net MVC, Submit Asp.net MVC form using GET and POST method, antiforgery token

Asp.net mvc form submit example

In asp.net mvc application, we can submit form in different ways, like we can submit formcollection fields or custom model using different submit methods post and get, let’s learn with example.

Form Submission in Asp.net MVC

In this article you will learn how to submit a Form in Asp.net MVC application using different methods, difference between get and post while submitting form, how to pass value through querystring etc.

To submit a form in MVC you have write the following code block in your razor.
@Html.BeginForm("actionName", "controllerName"){. form fields ..}

You can submit a form using two FormMethod, Post and Get. If you don't specify FormMethod.Post is default.

Submit Form using Post Method

This is an example form FormMethod.Post, if you submit this form, you won't see any value being passed on querystring.

Also after submitting this form once, if you (press F5) refresh the page, the form will be submitted again.

Your Name :

Here is the Form design code for FormMethod.Post

@using (Html.BeginForm("formsubmit", "aspnetmvc"))
{
    @Html.AntiForgeryToken()
    <div>Your Name : 
    <input type="text" id="txtFullName" /> </div>
    <div style="text-align: center"> 
    <input type="submit" value="Submit Form" /> </div>
}

This is how your controller will look like for FormMethod.Post

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult formsubmit(FormCollection form)
{
    string _txtFullName = form["txtFullName"];
    return View();
}

Note: if you are using model then in place of "FormCollection form" you can write "ModelName model"

Submit Form using Get Method

This is an example form FormMethod.Get, if you submit this form, you will see value being passed on querystring in url. Also after submitting this form once, if you (press F5) refresh the page, the form will not be submitted again

Your Name :

Here is the Form design code for FormMethod.Get

@using (Html.BeginForm("formsubmit", "aspnetmvc", FormMethod.Get))
{
@Html.AntiForgeryToken()
<div>Your Name :
    <input type="text" name="txtName" id="txtName" /> </div>
    <div style="text-align: center">
    <input type="submit" value="Submit Form" /> </div>
}

This is how your controller will look like for FormMethod.Get

[HttpGet]          
public ActionResult formsubmit()
{
    string _txtName = Request.QueryString["txtName"];
    return View();
}

Note: If you don't specify any ActionVerbs in controller, then default is HttpGet.

Use AntiForgeryToken in Asp.net MVC form

Make sure you use AntiForgeryToken in your razor page and in your controller as attribute [ValidateAntiForgeryToken] .

This AntiForgeryToken() token generates a hidden form field anti-forgery / anti-fake token, which is used in validation when the asp.net form is submitted.

There are two simple steps to implement this token in your form design, first add @Html.AntiForgeryToken() in your razor page form tag.

@using (Html.BeginForm("actionName", "controllerName", FormMethod.Post))
{
    @Html.AntiForgeryToken()
    // all fields goes here   
}

then add [ValidateAntiForgeryToken] attribute in your post action method in controller.

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult formsubmit(FormCollection formvalues)
{
    string _txtFullName = form["txtFullName"];
    return View();
}

This will generate hidden fields when the form is rendered in html code, that helps preventing CSRF (cross-site request forgery) attacks in asp.net mvc application, We also can add antiforgerytoken in ajax form submit in Asp.Net MVC application.

You also should learn

Asp.Net MVC C# Examples | Join Asp.Net MVC Course | Asp.net Core Tutorial