Getting Started With ASP.NET MVC - Part Four

Introduction

 
This article explains the basics of ASP.NET MVC and explains about the below topics -
  1. Different types of parameters
  2. Integrated View and Controller
  3. Integrated View and Controller using Model
Read the previous parts of the article before reading this one.

Different types of parameters

 
We can pass the different types of values through URL request using parameter variables in MVC.
 
Type 1
 
We do not pass any values with a URL request very often. For example, our URL is http://localhost:56302/Home/Index so our action method does not contain any parameter, it looks like below. 
  1. public class HomeController : Controller  
  2.     {  
  3.         public ActionResult Index()  
  4.         {  
  5.             return View();  
  6.         }  
  7.     }  
Type 2
 
Action method usually has parameters. Sometimes, if it has any parameter, we should pass the value through URL request based on a number of parameters. For example, here, only one parameter is in action method so our URL is “localhost:56302/Home/Index?a=10”. It looks like below. 
  1. public ActionResult Index(int a)  
  2.       {  
  3.           return View();  
  4.       }  
If the parameter is not nullable, we should pass the values to the parameter, otherwise, we might be getting an error. We can find the screenshot like below.
 
ASP.NET MVC 
 
Note
We should declare the parameter as nullable to avoid the error if we have not passed the value in the request URL. Normally, we mention the parameter as nullable by “?”. 
  1. public ActionResult Index(int? a)  
  2.         {  
  3.             return View();  
  4.         }  
Type 3
 
If we declare the string as a parameter, we do not need to pass the value as mandatory. Sting is a nullable value so it does not pass the value. On the other hand, it will take null values so we do not get an error. The functionality looks like below screenshot.
 
ASP.NET MVC 
 
Parameter value will pass to the action method if the values in URL looks like “localhost:56302/Home/Index?name="Vignesh".
 
ASP.NET MVC 
 

Integrated View and Controller

 
The view is a user interface to the user. The view should integrate with the corresponding controller to process the data. We are adding a new controller as “Demo” and adding an action method as “Customer”.
 
Example 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6.   
  7. namespace MVC_Part4.Controllers  
  8. {  
  9.     public class DemoController : Controller  
  10.     {  
  11.         // GET: Demo  
  12.         public ActionResult Customer()  
  13.         {  
  14.             return View();  
  15.         }  
  16.     }  
  17. }  
Add the view from “Customer” action method in the “Demo” controller, we can find the “Customer.cshtml” view page inside the view folder under “Demo” folder in MVC structure, looks like below screenshot.
 
ASP.NET MVC 
 
We are going to design as per our requirement, for example here we need to get the customer details, so we need to do a design that looks like the below user interface design.
 
Code for Design 
  1. @{  
  2.     ViewBag.Title = "Customer";  
  3. }  
  4.   
  5. <h2>Customer</h2>  
  6. @using (Html.BeginForm("SaveCustomer""Demo", FormMethod.Post))  
  7. {  
  8.     <table>  
  9.         <tr><td>Name</td><td>@Html.TextBox("Name")</td></tr>  
  10.         <tr><td>Address</td><td>@Html.TextBox("Address")</td></tr>  
  11.         <tr><td>City</td><td>@Html.TextBox("City")</td></tr>  
  12.         <tr><td>Mobile</td><td>@Html.TextBox("Mobile")</td></tr>  
  13.         <tr><td colspan="2"><input type="submit" value="Submit" /></td></tr>  
  14.     </table>  
  15. }  
Output
 
Once we run the application after designing,  using the URL http://localhost:50385/Demo/Customer, we get the output that looks like below.
 
ASP.NET MVC 
When we click submit, all the form values need to pass the controller but we need to integrate the specific View and controller action method.
 
We can integrate using “Html.BeginForm”, see the code below for map or integrated View and Controller action method. 
  1. @using (Html.BeginForm("SaveCustomer""Demo", FormMethod.Post))  
  2. {  
  3. }  
Above code integrates “Customer” view and “SaveCustomer” action method in Demo controller. This is the way of the integrated view and controller action methods. Now enter the values and click the submit button.
 
ASP.NET MVC 
 
After clicking submit, values pass to the controller but all field names and action method parameter names should be the same as below.
 
ASP.NET MVC 
 

Integrated View and Controller using Model

 
We can integrate view and controller using model. First create model as “Customer” inside the model folder, which looks like below.
 
ASP.NET MVC  
  1. namespace MVC_Part4.Models  
  2. {  
  3.     public class Customer  
  4.     {  
  5.         public string Name { set; get; }  
  6.         public string Address { set; get; }  
  7.         public string City { set; get; }  
  8.         public string Mobile { set; get; }  
  9.     }  
  10. }  
Add action method in “Demo” controller. 
  1. namespace MVC_Part4.Controllers  
  2. {  
  3.     public class DemoController : Controller  
  4.     {  
  5.         // GET: Demo  
  6.         public ActionResult Customer()  
  7.         {  
  8.             return View();  
  9.         }  
  10.     }  
  11. }  
Right click on the action method name and click “Add View”. Select “Create” in Template and select Model class “Customer (MVC_Part4.Models)” which creates a class inside the model folder.
 
ASP.NET MVC 
 
Now click the add button, this integration is with a model to view. After adding “Customer” view we can see the auto-generated code in the below. We changed only “@using (Html.BeginForm("SaveCustomer", "Demo",FormMethod.Post))” In the code. 
  1. @model MVC_Part4.Models.Customer  
  2. @{  
  3.     ViewBag.Title = "Customer";  
  4. }  
  5. <h2>Customer</h2>  
  6. @using (Html.BeginForm("SaveCustomer""Demo",FormMethod.Post))  
  7. {  
  8.     @Html.AntiForgeryToken()  
  9.       
  10.     <div class="form-horizontal">  
  11.         <h4>Customer</h4>  
  12.         <hr />  
  13.         @Html.ValidationSummary(true""new { @class = "text-danger" })  
  14.         <div class="form-group">  
  15.             @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })  
  16.             <div class="col-md-10">  
  17.                 @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })  
  18.                 @Html.ValidationMessageFor(model => model.Name, ""new { @class = "text-danger" })  
  19.             </div>  
  20.         </div>  
  21.   
  22.         <div class="form-group">  
  23.             @Html.LabelFor(model => model.Address, htmlAttributes: new { @class = "control-label col-md-2" })  
  24.             <div class="col-md-10">  
  25.                 @Html.EditorFor(model => model.Address, new { htmlAttributes = new { @class = "form-control" } })  
  26.                 @Html.ValidationMessageFor(model => model.Address, ""new { @class = "text-danger" })  
  27.             </div>  
  28.         </div>  
  29.   
  30.         <div class="form-group">  
  31.             @Html.LabelFor(model => model.City, htmlAttributes: new { @class = "control-label col-md-2" })  
  32.             <div class="col-md-10">  
  33.                 @Html.EditorFor(model => model.City, new { htmlAttributes = new { @class = "form-control" } })  
  34.                 @Html.ValidationMessageFor(model => model.City, ""new { @class = "text-danger" })  
  35.             </div>  
  36.         </div>  
  37.   
  38.         <div class="form-group">  
  39.             @Html.LabelFor(model => model.Mobile, htmlAttributes: new { @class = "control-label col-md-2" })  
  40.             <div class="col-md-10">  
  41.                 @Html.EditorFor(model => model.Mobile, new { htmlAttributes = new { @class = "form-control" } })  
  42.                 @Html.ValidationMessageFor(model => model.Mobile, ""new { @class = "text-danger" })  
  43.             </div>  
  44.         </div>  
  45.   
  46.         <div class="form-group">  
  47.             <div class="col-md-offset-2 col-md-10">  
  48.                 <input type="submit" value="Create" class="btn btn-default" />  
  49.             </div>  
  50.         </div>  
  51.     </div>  
  52. }  
  53.   
  54. <div>  
  55.     @Html.ActionLink("Back to List""Index")  
  56. </div>  
Now build and run our application and go to the http://localhost:50385/Demo/Customer URL and enter the values and click Create button,
 
ASP.NET MVC 
 
Once you click create button the controller goes to the corresponding controller's action method, and here there is no need to mention a separate variable for each field. Here we are using the model in the view and strongly typed HTML helper controller so in the action method only create an object for “Customer” class like below.
 
ASP.NET MVC 
 
All the values passed through an object because in the view all the values are integrated into the model properties. We pass Customer class object to the “SaveCustomer” view page. We can find the below code for the view details. Using “@model” we can integrate the model in the view page. 
  1. @model MVC_Part4.Models.Customer  
  2. @{  
  3.     ViewBag.Title = "SaveCustomer";  
  4. }  
  5. <h2>SaveCustomer</h2>  
  6. <div>  
  7.     <h4>Customer</h4>  
  8.     <hr />  
  9.     <dl class="dl-horizontal">  
  10.         <dt>  
  11.             @Html.DisplayNameFor(model => model.Name)  
  12.         </dt>  
  13.   
  14.         <dd>  
  15.             @Html.DisplayFor(model => model.Name)  
  16.         </dd>  
  17.   
  18.         <dt>  
  19.             @Html.DisplayNameFor(model => model.Address)  
  20.         </dt>  
  21.   
  22.         <dd>  
  23.             @Html.DisplayFor(model => model.Address)  
  24.         </dd>  
  25.   
  26.         <dt>  
  27.             @Html.DisplayNameFor(model => model.City)  
  28.         </dt>  
  29.   
  30.         <dd>  
  31.             @Html.DisplayFor(model => model.City)  
  32.         </dd>  
  33.   
  34.         <dt>  
  35.             @Html.DisplayNameFor(model => model.Mobile)  
  36.         </dt>  
  37.   
  38.         <dd>  
  39.             @Html.DisplayFor(model => model.Mobile)  
  40.         </dd>  
  41.   
  42.     </dl>  
  43. </div>  
  44. <p>  
  45.     @Html.ActionLink("Edit""Edit"new { /* id = Model.PrimaryKey */ }) |  
  46.     @Html.ActionLink("Back to List""Index")  
  47. </p>  
Build the code and run the application. Enter the value in demo page and submit; then view the submitted details.
 
Output
 
ASP.NET MVC 
 

Conclusion

 
This article explained different types of parameters, Integrated View and Controller, and Integrated View and Controller using Model. I hope this is very helpful for new learners. The next article explains about getting started with ASP.NET MVC part 5.


Similar Articles