WebTrainingRoom Online Courses
Online Training for Asp.net MVC Web Designing Development, MS SQL, Digital Marketing

How to submit Form in Asp.net MVC

How to submit Form in Asp.net MVC Application

In this article you will learn how to submit a Form in Asp.net MVC application, 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 FormMethod.Post in Asp.net MVC

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"))
{
<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]
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 FormMethod.Get in Asp.net MVC

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))
{
<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.

You also should learn Validation in Asp.Net MVC




Comment
Name Email Website
Subscribe
 
Form Submit in Asp.net MVC

Consulting