ASP.NET MVC Basic Application

Model View Controller (MVC) is a very easy concept. Let’s understand this in detail.

  1. A controller can send commands to the model to update the model's state (for example, editing a document). It can also send commands to its associated view to change the view's presentation of the model (for example, by scrolling through a document). It is an intermediate between the model and the view. It is usually a class (.cs) file.

  2. A model stores data that is retrieved to the controller and displayed in the view. Whenever there is a change to the data it is updated by the controller. Actually, it is made to create the Business Logic. It is a class (.cs) file.

  3. A view requests information from the model that it uses to generate an output representation to the user. It is a file with an extension (.cshtml).

  4. A view uses an engine called Razor View Engine. It is actually used to embed C# code in a HTML page. To use C# code in View page, prefix (@) symbol.

    mvc

Let’s understand the preceding theory using an example.

Step 1

Create an Empty ASP.NET MVC application in Visual Studio.

Create an Empty ASP application

empty website

Step 2

You will see that the Model, View, and Controller folders will be automatically created in the Solution Explorer.

Solution Explorer

Step 3

Add a model class file to the models folder. Name it Employee.cs.

add class

employee

Step 4

Create the auto-implemented properties inside it. Simply paste the following code.

  1. namespace MVCDemo.Models {  
  2.     public class Employee {  
  3.         public int EmployeeId {  
  4.             get;  
  5.             set;  
  6.         }  
  7.         public string Name {  
  8.             get;  
  9.             set;  
  10.         }  
  11.         public string Gender {  
  12.             get;  
  13.             set;  
  14.         }  
  15.         public string Salary {  
  16.             get;  
  17.             set;  
  18.         }  
  19.     }  
  20. }  

Step 5

Add a controller to the Controllers folder that will use the preceding created model class.

controller

controller name

Step 6

Copy the following code to use the model class’s property inside the controller.

  1. using MVCDemo.Models;  
  2. using System.Web.Mvc;  
  3.   
  4. namespace MVCDemo.Controllers   
  5. {  
  6.     public class EmployeeController: Controller   
  7.     {  
  8.         public ActionResult GetDetails()   
  9.         {  
  10.             Employee emp = new Employee();  
  11.             emp.EmployeeId = 1;  
  12.             emp.Name = "Ankit";  
  13.             emp.Gender = "Male";  
  14.             emp.Salary = "80000";  
  15.             return View(emp);  
  16.         }  
  17.   
  18.     }  
  19. }  

In this code, a namespace is added to use the model class. We just created the instance of the class Employee and initialized its properties. Then we returned that instance to the View, because we will show this data on the View page. So, the view will show the data contained in this instance.

Step 7

Right-click on the GetDetails method and add a View.

view name

Remember to build the project using Solution Explorer, otherwise the Model class in the preceding screenshot will not be available in the dropdown menu.

Step 8

A view will be added to the Views folder.

GetDetals

Step 9

Add the following code to the GetDetals.cshtml.

  1. @model MVCDemo.Models.Employee  
  2.   
  3. @{  
  4. ViewBag.Title = "Employee Details";  
  5. }  
  6.   
  7.   
  8. <h2>Employee Details</h2>  
  9. <table>  
  10.     <tr>  
  11.         <td>  
  12.             <b>Employee Id is : </b>  
  13.         </td>  
  14.         <td>  
  15. @Model.EmployeeId  
  16. </td>  
  17.     </tr>  
  18.     <tr>  
  19.         <td>  
  20.             <b>Name is : </b>  
  21.         </td>  
  22.         <td>  
  23. @Model.Name  
  24. </td>  
  25.     </tr>  
  26.     <tr>  
  27.         <td>  
  28.             <b>Gender is : </b>  
  29.         </td>  
  30.         <td>  
  31. @Model.Gender  
  32. </td>  
  33.     </tr>  
  34.     <tr>  
  35.         <td>  
  36.             <b>Salary is : </b>  
  37.         </td>  
  38.         <td>  
  39. @Model.Salary  
  40. </td>  
  41.     </tr>  
  42. </table>  

Step 10

Just press F5 and navigate to the URL.

In the preceding URL, Employee is the name of model and GetDetails is the name of View that is included in this model. This View searches for the GetDetails method in the Controller. If it finds it in the controller, then the data will be shown, otherwise a 404 error will occur. So, please be sure that the name of the view and the function in the controller are same.

employee detail

If you like this article, please provide you comment and views. See this article on my personal blog.

Up Next
    Ebook Download
    View all
    Learn
    View all