You are on page 1of 9

1/6/2019 ASP.

Net MVC: Form Submit (Post) example

ASP.Net
ASP.Net MVC:
MVC: Form
Form Submit
Submit (Post)
(Post) example
example
26 Aug 2016 26 Aug 2016 Mudassar Khan 5 Comments 132675 Views
ASP.Net MVC
YouTube 392

Like 13K

Follow @ASPSnippets

Here Mudassar Ahmed Khan has explained with an example, how to submit (post) a Form and send data fro
m View to Controller in ASP.Net MVC 5.
This article will explain how to create Form Fields using Model class and then send data from View to Controll
er using Model class object in ASP.Net MVC 5.

Download Download Free Files API

https://www.aspsnippets.com/Articles/ASPNet-MVC-Form-Submit-Post-example.aspx 1/9
1/6/2019 ASP.Net MVC: Form Submit (Post) example

In this article I will explain with an example, how to submit (post) a Form and send data from View to Controller in ASP.N
et MVC 5.

This article will explain how to create Form Fields using Model class and then send data from View to Controller using M
odel class object in ASP.Net MVC 5.

Note: For beginners in ASP.Net MVC, please refer my article ASP.Net MVC Hello World Tutorial with Sample
Program example.

Model

Following is a Model class named PersonModel with four properties i.e. PersonId, Name, Gender and City.
public class PersonModel
{
///<summary>
/// Gets or sets PersonId.
///</summary>
public int PersonId { get; set; }

///<summary>
/// Gets or sets Name.
///</summary>
public string Name { get; set; }

///<summary>
/// Gets or sets Gender.
///</summary>
public string Gender { get; set; }

///<summary>
/// Gets or sets City.
///</summary>
public string City { get; set; }
}

Controller
Then you will need to add a Controller class to your project. There are two Action methods with the name Index, one for
handling the GET operation while other for handling the POST operation.
The Action method for POST operation accepts an object of the PersonModel class as parameter. The values posted fro
m the Form inside the View are received through this parameter.
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
return View();
}

https://www.aspsnippets.com/Articles/ASPNet-MVC-Form-Submit-Post-example.aspx 2/9
1/6/2019 ASP.Net MVC: Form Submit (Post) example

[HttpPost]
public ActionResult Index(PersonModel person)
{
int personId = person.PersonId;
string name = person.Name;
string gender = person.Gender;
string city = person.City;

return View();
}
}

View
Next step is to add a View for the Controller and while adding you will need to select the PersonModel class created earli
er.

Inside the View, in the very first line the PersonModel class is declared as Model for the View.
The View consists of an HTML Form which has been created using the Html.BeginForm method with the following param
eters.
ActionName – Name of the Action. In this case the name is Index.
ControllerName – Name of the Controller. In this case the name is Home.

FormMethod – It specifies the Form Method i.e. GET or POST. In this case it will be set to POST.
There are three TextBox fields created for capturing values for PersonId, Name and City using the Html.TextBoxFor meth
od. While for capturing the Gender value, a DropDownList with three options is created using the Html.DropDownListFor
function.
There’s also a Submit Button at the end of the Form and when the Button is clicked, the Form is submitted.
@model Form_Post_MVC.Models.PersonModel

@{
Layout = null;
}

<!DOCTYPE html>

https://www.aspsnippets.com/Articles/ASPNet-MVC-Form-Submit-Post-example.aspx 3/9
1/6/2019 ASP.Net MVC: Form Submit (Post) example
<html>
<head>
<meta name="viewport" content="width=device-width"/>
<title>Index</title>
</head>
<body>
@using (Html.BeginForm("Index", "Home", FormMethod.Post))
{
<table cellpadding="0" cellspacing="0">
<tr>
<th colspan="2" align="center">Person Details</th>
</tr>
<tr>
<td>PersonId: </td>
<td>
@Html.TextBoxFor(m => m.PersonId)
</td>
</tr>
<tr>
<td>Name: </td>
<td>
@Html.TextBoxFor(m => m.Name)
</td>
</tr>
<tr>
<td>Gender: </td>
<td>
@Html.DropDownListFor(m => m.Gender, new List<SelectListItem>
{ new SelectListItem{Text="Male", Value="M"},
new SelectListItem{Text="Female", Value="F"}}, "Please select")
</td>
</tr>
<tr>
<td>City: </td>
<td>
@Html.TextBoxFor(m => m.City)
</td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Submit"/></td>
</tr>
</table>
}
</body>
</html>

Screenshots
The Form

https://www.aspsnippets.com/Articles/ASPNet-MVC-Form-Submit-Post-example.aspx 4/9
1/6/2019 ASP.Net MVC: Form Submit (Post) example

Values captured in Controller when Form is submitted

Downloads

Download
Download Free Files API

https://www.aspsnippets.com/Articles/ASPNet-MVC-Form-Submit-Post-example.aspx 5/9
1/6/2019 ASP.Net MVC: Form Submit (Post) example

Comments

Smithk250Oct 13, 2016 04:10 AM 127.0.0.1

I just couldnt depart your site before suggesting that I actually enjoyed the standard
information a person provide for your visitors Is gonna be back often in order to check up on
new posts dakcfkkgcefcckbk

https://www.aspsnippets.com/Articles/ASPNet-MVC-Form-Submit-Post-example.aspx 6/9
1/6/2019 ASP.Net MVC: Form Submit (Post) example

sreejithJun 20, 2017 04:06 AM 117.247.186.143

nice article

HarunNov 14, 2017 08:11 AM 212.174.195.23

Thank you very much for this example.

JasnaApr 05, 2018 07:04 AM 213.130.121.187

Such a simple and wonderful example

NanoMay 14, 2019 01:05 AM 115.74.36.192

nice post thank you so much

Add Comments

https://www.aspsnippets.com/Articles/ASPNet-MVC-Form-Submit-Post-example.aspx 7/9
1/6/2019 ASP.Net MVC: Form Submit (Post) example

You can add your comment about this article using the form below. Make sure you provide a valid email
address
else you won't be notified when the author replies to your comment
Please note that all comments are moderated and will be deleted if they are

Not relavant to the article


Spam
Advertising campaigns or links to other sites
Abusive content.

Please do not post code, scripts or snippets.

YouTube 392

Like 13K

Follow @ASPSnippets

Name
Name
Email
Email
Comment
Comment

Security code:

I declare, I accept the site's Privacy Policy.

Add Comment

What our readers say

https://www.aspsnippets.com/Articles/ASPNet-MVC-Form-Submit-Post-example.aspx 8/9
1/6/2019 ASP.Net MVC: Form Submit (Post) example

Bharanikumar
Super and simple explanation easy for beginners . awesome
Ameer Abulawi
This is Great. This website Rocks.
Thank you Mudassar
Creis Jean
your articles are so helpful and so well explained.
thank you so much for all your work.
JP
Kumaresan
Great collections of code
Raymond Moore
You just saved me a weeks worth of work.
This is an excellent guide and works just like in the demo. I especially like the way you used SQL paging and
avoided querying the same data twice by using a temp table.
Daniel Waduka
This is my second time to use your blog but surely this is the best online guide for asp.net. Very precise clear and
accurate. Thanks
Sunil
Simple and clear expecting these type answers for slow learners like me.
Aman Jain
Sir you are doing really nice work. I have learnt lot of things from your website and the way you present source
code and demo is really wonderful.
Thank you very much sir.
Keep it up.
Sarada
Thanks for the simple solutions.
Victoria
Thank you very much From Russia with love

© 2019 www.aspsnippets.com All rights reserved | Privacy Policy | Powered by Excelasoft Solutions

Disclaimer
This site makes use of Cookies. Please refer Privacy Policy for more details.
Got it

https://www.aspsnippets.com/Articles/ASPNet-MVC-Form-Submit-Post-example.aspx 9/9

You might also like