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.
- Razor Engine (CSHTML)
- 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.
- public ActionResult Index()
- {
- return View();
- }
- [HttpPost]
- public ActionResult Index([Bind(Include = "age,name")] Student std)
- {
- if (!ModelState.IsValid)
- {
- if (std.name == null)
- {
- ModelState.AddModelError("", "Please Enter the name..");
- }
- return View();
- }
- int age = std.age;
- string name = std.name;
- return View();
- }
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.
- public static void RegisterRoutes(RouteCollection routes)
- {
- routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
-
- routes.MapRoute(
- name: "Default",
- url: "{controller}/{action}/{id}",
- defaults: new { controller = "Student", action = "Index", id = UrlParameter.Optional }
- );
- }
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.
- @model Sample_MVC_App.Models.Student
-
- @{
- ViewBag.Title = "Index";
- }
-
- <h2>Index</h2>
-
- @using (Html.BeginForm("Index","Student"))
- {
- @Html.ValidationSummary(true)
- @Html.DropDownList("studGraduate",new SelectList(Enum.GetValues(typeof(Sample_MVC_App.Models.Student.Graduate))),"Select Graduate", new {@class="form-control"})
- @Html.Password("password12")
- <br />
- @Html.Label("Age")
- @Html.EditorFor(s => s.age)
- <br />
- @Html.Label("name")
- @Html.TextBoxFor(s => s.name )
- <br />
- @Html.TextBox("address")
- @Html.ValidationMessage("address")
- <input type="submit" value="Save" />
- }
I am trying to make you to understand How MVC is differentiated from three tier architecture. Don’t think both approaches are the same.
- Presentation Layer is the same as View in MVC. (To display output to user)
- 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.
- [Required]
- public string address { get; set; }
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.