4 Ways To Create Form in ASP - NET MVC
4 Ways To Create Form in ASP - NET MVC
NET MVC
Forms are very essential and basic thing that every programmer has to learn. In this tutorial, I will teach you 4
Different Ways to Create ASP.NET MVC Forms with ease.
Setup
Step 1: Create a New ASP.NET MVC Project MvcForms. Go to File New Project. If you don’t know
how to create a new mvc project then see this chapter.
1 namespace MvcForms.Models
2 {
3 public class StudentModel
4 {
5 public int Id { get; set; }
6 public string Name { get; set; }
7 public bool Addon { get; set; }
8 }
9 }
1 [HttpPost]
2 public ActionResult form1(int txtId, string txtName, string chkAddon)
3 {
4 ViewBag.Id = txtId;
5 ViewBag.Name = txtName;
6 if (chkAddon != null)
7 ViewBag.Addon = "Selected";
8 else
9 ViewBag.Addon = "Not Selected";
10
11 return View("Index");
12 }
Output:
(https://www.completecsharptutorial.com/wp-content/uploads/2017/11/New-Picture-1-2.png)
Let’s Understand
1. In the <form action="form1" method=”post”>, form1 is Action Method that gets executed when forms sends
data to HomeController using post method. In the next chapter, you will learn about post and get method in mvc.
2. In the <input type="text" name="txtId" />, the property name=”txtId” must be same as parameter name
in form 1 action method.
(https://www.completecsharptutorial.com/wp-content/uploads/2017/11/weakly-typed-form.jpg)
3. CheckBox sends "on" if it selected otherwise sends null.
Disadvantage:
In this method, we send objects (model) instead of sending each item as parameter. It is easy to maintain because
you don't need to remember each input item and IntelliSense will show you automatically the each item.
(https://www.completecsharptutorial.com/wp-content/uploads/2017/11/New-Picture-3-2.png)
1 [HttpPost]
2 public ActionResult Form2(Models.StudentModel sm)
3 {
4 ViewBag.Id = sm.Id;
5 ViewBag.Name = sm.Name;
6 if (sm.Addon == true)
7 ViewBag.Addon = "Selected";
8 else
9 ViewBag.Addon = "Not Selected";
10
11 return View("Index");
12 }
Output
(https://www.completecsharptutorial.com/wp-content/uploads/2017/11/New-Picture-4-2.png)
Let’s understand it
In the Form
1. @using (Html.BeginForm("Form2", "Home", FormMethod.Post)) is used for creating strongly typed forms. It
has 3 parameters that denotes:
i. Form2: It is Action Method Name
ii. Home: It is Controller Name
iii. FormMethod.Post: It denotes that all the data will be submitted to controller using Post method.
iv. @Html.TextBoxFor(m => m.Id) This is Html Helper. I have created textbox using mvc htmo helper and it is
strongly bounded with Id.
v. m => m.Id is a lambda expression. It means that m is an instance of StudentModel.
In the Form2 Action Method in HomeController
Asynchronous AJAX form is a very magical way to submit data to the controller without happening page load.
Asynchronous AJAX Forms simply post back the data to the controllers and update the only that part of the page,
which has to display output.
To make this happen, we will use JQuery-Unobstrusive-AJAX. This is a great feature which is launched in MVC 3. It
helps you to create AJAX Form without writing bunch of javascript code. Before creating Asynchronous AJAX Form
you need to add JQuery-Unobstrusive-AJAX in your project. Adding is very easy, and just follows the steps.
Adding JQuery-Unobstrusive-AJAX
Step 1: Right-click on your Project name in Solution Explorer and click Manage Nuget Packages…
Step 2: Go to Browse and search for ajax. Find and Install Microsoft-JQuery-Unobstrusive-Ajax.
(https://www.completecsharptutorial.com/wp-content/uploads/2017/11/New-Picture-7.png)
(https://www.completecsharptutorial.com/wp-content/uploads/2017/11/New-Picture-8.png)
Now, your project is ready to use JavaScript and AJAX.
Create Forms and Controller.
1 @model MvcForms.Models.StudentModel
2 <script src="@Url.Content("~/Scripts/jquery-1.10.2.min.js")" type="text/javascript"></scr
3 <script src="@Url.Content("~/Scripts/jquery.unobtrusive-ajax.js")" type="text/javascript"
4
5 <h4 id="id1" style="color:purple"></h4>
6 <hr />
7 <h3><b>Forms - Strongly Typed AJAX (Asynchronous)</b></h3>
8 @using (Ajax.BeginForm("Form3", "Home", new AjaxOptions
9 {
10 HttpMethod = "POST",
11 UpdateTargetId = "id1",
12 LoadingElementId = "LoadingImage",
13 OnSuccess = "onSuccess_Message",
14 OnFailure="onFailure_Message"
15
16 }))
17 {
18 <table>
19 <tr>
20 <td>Enter ID: </td>
21 <td>@Html.TextBoxFor(m => m.Id)</td>
22 </tr>
23 <tr>
24 <td>Enter Name: </td>
25 <td>@Html.TextBoxFor(m => m.Name)</td>
26 </tr>
27 <tr>
28 <td>Addon: </td>
29 <td>@Html.CheckBoxFor(m => m.Addon)</td>
30 </tr>
31 <tr>
32 <td colspan="2"><input type="submit" value="Submit Form" /></td>
33 </tr>
34 </table>
35 <div id="LoadingImage" style="display:none">Loading...</div>
36 <div id="onSuccess_Message"></div>
37 <div id="onFailure_Message"></div>
38 }
You must check the correct version of javascript installed on your project. Jquery-1.xx.x.
2.
(https://www.completecsharptutorial.com/wp-content/uploads/2017/11/New-Picture-9.png)
In this method, you can not only send data from input controls but can also use html elements like <p>…</p>,
<span>…</span> to send data to controllers. This is pure JQuery and AJAX query.
Create Forms
Step 1. Go to Index.cshtml and create form like this.
(https://www.completecsharptutorial.com/wp-content/uploads/2017/11/New-Picture-12-copy.png)
(https://www.completecsharptutorial.com/wp-content/uploads/2017/11/New-Picture-10.png)
SUMMARY
In this tutorial, you learned 4 different ways to create form and submit data to the controller. All these 4 ways used
widely in MVC and I hope now you will be able to create a form in ASP.NET MVC. In the next chapter, you will learn
FormCollection object in details with programming example. FormCollection objects make a job much easier
when collecting form data into the controller.
(https://www.completecsharptutorial.com/asp-net-mvc5/formcollection-object-in-asp-net-
mvc-with-example.php)