ASP.NET MVC 5 - First Web Application: Part 1

ASP.NET MVC is a web development framework or architecture from Microsoft that combines the tidiness and effectiveness of model view controller (MVC) architecture which is the most up - to - date techniques from agile development. It is a complete alternate technology of traditional ASP.NET Web Forms. In this article, we will discuss about the ASP.NET MVC 5 with its new features. Also learn how to develop your first program in MVC 5.

Some disadvantages of Traditional ASP.NET Web Forms:

  • View State weight
  • Page life cycle
  • False sense of separation of concern
  • Limited control over HTML

In today’s development world, web development technology has been progressing rapidly. Now a days web sites and web based applications are used on variety of devices and browsers than ever before. Also after introducing HTML5, it has begun to enter mainstream use and provides the web developer with rich capabilities that allow the client to perform work that used server side earlier.

Main benefits of ASP.NET MVC

In October 2007, Microsoft announced a new MVC Web development platform, built on the core ASP.NET platform, specially designed as a direct response to the evolution of technologies.

  1. MVC Architecture: It is important to distinguish between the MVC architecture pattern and the ASP.NET MVC Framework. Actually MVC pattern is not new - it dates back to 1978 and the Smalltalk project at Xerox PARC - it achieved its most popularity today as a pattern for web development because user interactive with an MVC application follows a natural cycle : the user takes an action and in response the application changes its data model and delivers an updated view to the user. And then the cycle repeats. This is a convenient fit for Web Applications delivered as a series of HTTP requests and response.

  2. Extensibility: MVC framework is developed on basis of independent components that satisfy a .NET interface or that are built on an abstract base class. We can easily replace components such as the routing system, the view engine and the controller factory, with a different one of your own implementation.

  3. Strong control over HTML and HTTP: ASP.NET MVC produces clean, standard-compliant markup. Its built in HTML helper methods produce a standards - compliant output, but there is a more significant change compared with web forms.

  4. Testability: The MVC architecture gives us a great start in making your application maintainable and testable because you normally separate your application into different independent parts. Also to support Unit testing, it took the framework's component oriented design and made sure that each separate piece is structured to meet the requirements of unit testing and mocking tools.

  5. Routing System: The style of urls developed as a web application technology has improved. In web application, urls are looks like the following: App_code/Project/HomePage.aspx?user_id=xyz 
           Whereas in MVC technology, it is now very much simpler and clean 
            Login/HomeIndex 
 
 Now, how to create your first application in MVC 5. For this, we first open Visual Studio 2013 and click on File, New, then Projects


Provide project name and click on OK button.


Click on MVC as per the above image and click ok. Your MVC Project has been created. You can run the project which will show you a sample template of MVC project. But we create our own custom Page.

Now in your solution explorer, there is a folder name Controller. Select the folder and right click the mouse and click on Add a Controller.


Select MVC 5 Empty Controller and Click on Add Button.


Type controller name as Student and click on Ok.

The StudentController creates with default action method named Index which return ActionResult. We change the return type to ViewResult.

  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_Article.Controllers  
  8. {  
  9.     public class StudentController : Controller  
  10.     {  
  11.         // GET: Student  
  12.         public ViewResult Index()  
  13.         {  
  14.             return View();  
  15.         }  
  16.     }  
  17. }  
Now right click on Index and click on Add View and then Click on Ok.

 
 
Now right the following code in index.cshtml page.
  1. @{  
  2.     ViewBag.Title = "Index";  
  3. }  
  4.   
  5. <html>  
  6. <head>  
  7.     <meta name="viewport" content="width=device-width" />  
  8.     <title> Student Add</title>  
  9.     <link href="~/Content/bootstrap.css" rel="stylesheet" />  
  10.     <script src="~/Scripts/jquery-2.1.4.min.js"></script>  
  11.     <script src="~/Scripts/bootstrap.min.js"></script>  
  12. </head>  
  13. <body>  
  14.     <h2>Student Database Project</h2>  
  15.     <div class="btn-link">         
  16.         @Html.ActionLink("Register Student", "StudentAdd")  
  17.     </div>  
  18. </body>  
  19. </html>  
Actually, we want to create a Student Registration form. For this, first we need to define the student model class. So we will add a Student Class under Model folder and define the student class. 
  1. public class Student  
  2.     {  
  3.         public int StudentId { getset; }  
  4.         public string StudentName { getset; }  
  5.         public string FatherName { getset; }  
  6.         public DateTime DateOfBirth { getset; }  
  7.         public string Address { getset; }  
  8.         public string Class { getset; }  
  9.         public int RollNo { getset; }  
  10.     }  
Now in the StudentController, we add another viewresult method named StudentAdd() and also add a view against this method with strongly type model.
  1. public ViewResult StudentAdd()  
  2.         {  
  3.             return View();  
  4.         }  
The following is the code of StudentAdd view:
  1. @model MVC_Article.Models.Student  
  2.   
  3. @{  
  4.     Layout = null;  
  5. }  
  6.   
  7. <!DOCTYPE html>  
  8.   
  9. <html>  
  10. <head>  
  11.     <meta name="viewport" content="width=device-width" />  
  12.     <title> Student Add</title>  
  13.     <link href="~/Content/bootstrap.css" rel="stylesheet" />  
  14.     <script src="~/Scripts/jquery-2.1.4.min.js"></script>  
  15.     <script src="~/Scripts/bootstrap.min.js"></script>  
  16. </head>  
  17. <body>  
  18.     @using (Html.BeginForm())  
  19.     {  
  20.         @Html.AntiForgeryToken()  
  21.   
  22.         <div class="form-horizontal">  
  23.             <h2>Register New Student</h2>  
  24.             <hr />  
  25.             @Html.ValidationSummary(true, "", new { @class = "text-danger" })  
  26.             <div class="form-group">  
  27.                 @Html.LabelFor(model => model.StudentName, htmlAttributes: new { @class = "control-label col-md-2" })  
  28.                 <div class="col-md-10">  
  29.                     @Html.EditorFor(model => model.StudentName, new { htmlAttributes = new { @class = "form-control" } })  
  30.                     @Html.ValidationMessageFor(model => model.StudentName, "", new { @class = "text-danger" })  
  31.                 </div>  
  32.             </div>  
  33.   
  34.             <div class="form-group">  
  35.                 @Html.LabelFor(model => model.FatherName, htmlAttributes: new { @class = "control-label col-md-2" })  
  36.                 <div class="col-md-10">  
  37.                     @Html.EditorFor(model => model.FatherName, new { htmlAttributes = new { @class = "form-control" } })  
  38.                     @Html.ValidationMessageFor(model => model.FatherName, "", new { @class = "text-danger" })  
  39.                 </div>  
  40.             </div>  
  41.   
  42.             <div class="form-group">  
  43.                 @Html.LabelFor(model => model.DateOfBirth, htmlAttributes: new { @class = "control-label col-md-2" })  
  44.                 <div class="col-md-10">  
  45.                     @Html.EditorFor(model => model.DateOfBirth, new { htmlAttributes = new { @class = "form-control" } })  
  46.                     @Html.ValidationMessageFor(model => model.DateOfBirth, "", new { @class = "text-danger" })  
  47.                 </div>  
  48.             </div>  
  49.   
  50.             <div class="form-group">  
  51.                 @Html.LabelFor(model => model.Address, htmlAttributes: new { @class = "control-label col-md-2" })  
  52.                 <div class="col-md-10">  
  53.                     @Html.EditorFor(model => model.Address, new { htmlAttributes = new { @class = "form-control" } })  
  54.                     @Html.ValidationMessageFor(model => model.Address, "", new { @class = "text-danger" })  
  55.                 </div>  
  56.             </div>  
  57.   
  58.             <div class="form-group">  
  59.                 @Html.LabelFor(model => model.Class, htmlAttributes: new { @class = "control-label col-md-2" })  
  60.                 <div class="col-md-10">  
  61.                     @Html.DropDownListFor(x => x.Class, new[] {  
  62.                     new SelectListItem() {Text="BCA",Value="BCA"},  
  63.                     new SelectListItem() {Text="BBA",Value="BBA"},  
  64.                     new SelectListItem() {Text="BTECH",Value="BTECH"},  
  65.                     new SelectListItem() {Text="BCA",Value="MCA"},  
  66.                     new SelectListItem() {Text="BBA",Value="MBA"},  
  67.                     new SelectListItem() {Text="BTECH",Value="MTECH"}  
  68.                })  
  69.                     @Html.ValidationMessageFor(model => model.Class, "", new { @class = "text-danger" })  
  70.                 </div>  
  71.             </div>  
  72.   
  73.             <div class="form-group">  
  74.                 @Html.LabelFor(model => model.RollNo, htmlAttributes: new { @class = "control-label col-md-2" })  
  75.                 <div class="col-md-10">  
  76.                     @Html.EditorFor(model => model.RollNo, new { htmlAttributes = new { @class = "form-control" } })  
  77.                     @Html.ValidationMessageFor(model => model.RollNo, "", new { @class = "text-danger" })  
  78.                 </div>  
  79.             </div>  
  80.   
  81.             <div class="form-group">  
  82.                 <div class="col-md-offset-2 col-md-10">  
  83.                     <input type="submit" value="Create" class="btn btn-success" />  
  84.                 </div>  
  85.             </div>  
  86.         </div>  
  87.     }  
  88.   
  89.     <div>  
  90.         @Html.ActionLink("Back to Home", "Index")  
  91.     </div>  
  92. </body>  
  93.   
  94. </html>  
Now after clicking on CREATE button, it will redirect to a confirmation page. For this, we change the studentcontroller as in the following code snippet: 
  1. using MVC_Article.Models;  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Linq;  
  5. using System.Web;  
  6. using System.Web.Mvc;  
  7.   
  8. namespace MVC_Article.Controllers  
  9. {  
  10.     public class StudentController : Controller  
  11.     {  
  12.         // GET: Student  
  13.         public ViewResult Index()  
  14.         {  
  15.             return View();  
  16.         }  
  17.   
  18.         [HttpGet]  
  19.         public ViewResult StudentAdd()  
  20.         {  
  21.             return View();  
  22.         }  
  23.   
  24.         [HttpPost]  
  25.         public ViewResult StudentAdd(Student studentData)  
  26.         {  
  27.             return View("ConfirmStudent", studentData);  
  28.         }  
  29.     }  
  30. }  
Now Add a ConfirmStudent Named view and add the following code:
  1. @model MVC_Article.Models.Student  
  2.   
  3. @{  
  4.     Layout = null;  
  5. }  
  6.   
  7. <!DOCTYPE html>  
  8.   
  9. <html>  
  10. <head>  
  11.     <meta name="viewport" content="width=device-width" />  
  12.     <title> Student Registration Confirm</title>  
  13.     <link href="~/Content/bootstrap.css" rel="stylesheet" />  
  14.     <script src="~/Scripts/jquery-2.1.4.min.js"></script>  
  15.     <script src="~/Scripts/bootstrap.min.js"></script>  
  16. </head>  
  17. <body>  
  18.     @using (Html.BeginForm())  
  19.     {  
  20.         @Html.AntiForgeryToken()  
  21.   
  22.         <div class="form-horizontal">  
  23.             <h2>Confirm Register</h2>  
  24.             <hr />  
  25.             <div>  
  26.                 <h1>Thanks @Model.StudentName !</h1>  
  27.                 <h3>Your Registration has been complete.</h3>  
  28.             </div>  
  29.         </div>  
  30.     }  
  31.   
  32.     <div>  
  33.         @Html.ActionLink("Back to Home", "Index")  
  34.     </div>  
  35. </body>  
  36.   
  37. </html>  
Now run the application and it will show the following:
 
 
 
 

Next Recommended Readings