Introduction To MVC

Introduction

Hi all, after I posted two articles, a few people requested that I  post about MVC. We have a lot of tutorials about MVC. But they all explain things in technical terms, and it's not easy to understand things from those articles. I am going to explain only about the basics of MVC. I don't want to go in depth in all concepts in MVC. First you need to understand: What is MVC? Why do we need MVC? Once you understand those points then, you can go through all the tutorials.

What is MVC?

I don't want to give the definition of MVC. Here I am explaining in a  practical way for easy understanding.

IS MVC a design pattern? IS MVC a language? IS MVC a new technology? The answer is yes for all. MVC is an abbreviation for Model View Controller.

MVC is a pure HTML development environment.

View

View is used to display the output to the user. Rendering will happen in View. We have two kinds of view engines.

  1. Razor Engine (CSHTML)
  2. ASPX Engine (ASPX)

Model

It contains business flow of an application. We can write all our business logic in Model. We can maintaina data base connection in Model.

Controller

One of the main features of MVC is controller. Controller handles user requests.

  1. public ActionResult Index()  
  2. {  
  3.     return View();  
  4. }  
  5. [HttpPost]  
  6. public ActionResult Index([Bind(Include = "age,name")] Student std)  
  7. {  
  8.     if (!ModelState.IsValid)  
  9.     {  
  10.         if (std.name == null)  
  11.         {  
  12.             ModelState.AddModelError("""Please Enter the name..");  
  13.         }  
  14.         return View();  
  15.     }  
  16.     int age = std.age;  
  17.     string name = std.name;  
  18.     return View();  
  19. }  
Example

Consider Traditional Web Development -- if we request a URL from the server, it will match it with a particular physical location to find the file.

www.student/student_registarion.aspx

If we request student_registarion page, the web server searches the main folder for the physical file which is "Student_Registarion" Page. If the physical file is not found, it throws error 404 Not FOUND.

Routing

Routing is a main feature in MVC. Whenever we request  a URL, it maps with controller. Controller handles the user request. Here there is no concept of physical file mapping. You will have a  function for handling the user requests, and selecting the model for getting data from the database if required and choosing the view to display to end users.
  1. public static void RegisterRoutes(RouteCollection routes)  
  2. {  
  3.     routes.IgnoreRoute("{resource}.axd/{*pathInfo}");  
  4.   
  5.     routes.MapRoute(  
  6.         name: "Default",  
  7.         url: "{controller}/{action}/{id}",  
  8.         defaults: new { controller = "Student", action = "Index", id = UrlParameter.Optional }  
  9.     );  
  10. }  


You are familiar with three tier architecture and if you are not aware of MVC don't worry. I can compare it with three tier architecture below.

Three Tier architecture with MVC (Comparison)

The main difference, as I explained above, is the file mapping in Routing. The second one is loosely coupled, whereas three tier is tightly coupled architecture. If you click the button there is no need to create the object for business layer like in three tier; here all the values are posted to controller. Controller chooses the model for processing the input data and responds to request.
  1. @model Sample_MVC_App.Models.Student  
  2.   
  3. @{  
  4.     ViewBag.Title = "Index";  
  5. }  
  6.   
  7. <h2>Index</h2>  
  8.   
  9. @using (Html.BeginForm("Index","Student"))  
  10. {  
  11.     @Html.ValidationSummary(true)  
  12.     @Html.DropDownList("studGraduate",new SelectList(Enum.GetValues(typeof(Sample_MVC_App.Models.Student.Graduate))),"Select Graduate"new {@class="form-control"})  
  13.     @Html.Password("password12")  
  14.     <br />  
  15.     @Html.Label("Age")  
  16.     @Html.EditorFor(s => s.age)  
  17.     <br />  
  18.     @Html.Label("name")  
  19.     @Html.TextBoxFor(s => s.name )  
  20.     <br />  
  21.     @Html.TextBox("address")  
  22.     @Html.ValidationMessage("address")  
  23.     <input type="submit" value="Save" />  
  24. }  
I am trying to make you to understand How MVC is differentiated from three tier architecture. Don’t think both approaches are the same.
  1. Presentation Layer is the same as View in MVC. (To display output to user)
  2. Business Layer is the same as Model in MVC. (To handle Business Flow)

Here we don't have the same functionality as Data access Layer. We can maintain all the data base connections in model or entity framework.

Validation

We have a set of validation in traditional web development that we can set in view model. What is view model? View model is class,so that we can set validation for each activity by using data annotation. There is no validation happening in view like in three tier architecture. It is all set in view model.

  1. [Required]  
  2. public string address { getset; }  
Conclusion

MVC is an ocean. I am explaining a drop of it. I hope it's an easy way to understand  what is MVC. I will explain later each concept in detail. I wish you all the best to read and understand the basics of MVC.

 


Similar Articles