SUBJECT
KNOW WHAT IS MVC AND DEVELOP A SIMPLE APLICATION
CONTENT
OUR AGENDA:
- Introduction To MVC
- Why Asp.net MVC over Asp.net Web Forms
- MVC Architecture-Overview
- MVC Request Life Cycle
- Basic Example –Explain
INTRODUCTION TO MVC:
MVC Stands for MODEL VIEW CONTROLLER. It is a “Standard design Pattern”
MVC Versions are:
Why ASP.NET MVC over ASP.NET WebForms :
- There is a clear separation between your UI and Code and Business Logic
- Enables full control over rendered HTML, JavaScript and CSS
- Easy integration with JavaScript Frameworks (like jQuery).
- No Server side controls, No Post back's, No View State. So that, improves performance also.
- Enables a Perfect URL Routing; hence the URL will be user-friendly and SEO friendly.
- Ease for Unit testing
MVC Architecture :
Model (Data Access Layer) :
- A model is a class, which provides necessary structure of the data, using properties.
- In real time applications, we map the models with databases.
EX:
class CustomerModel
{
public int CustomerID { get; set; }
public string CustomerName { get; set; }
public string Phone { get; set; }
}
Very Important:
- A model should be called by the controller / view.
- A model can't call the controller / view also.
Controller (Business Access Layer) :
- A Controller is a class, which accepts the request from browser, access the data from databases (using models), and also provides the necessary data to the view.
EX:
class CustomerController : Controller
{
public void ViewCustomer()
{
//receive database data using
//call the view and pass necessary data to view
}
}
Very Important:
- A controller can receive the request from the client (browser).
- A controller can call the view and model directly.
- A controller's common responsibility is: to fetch the data from model and serve it to the view.
- A controller can't be called by the view / model.
View (Presentation Layer) :
- Views are the components that display the application's user interface (UI).
EX:
Customer.cshtml
<div>
@Model.CustomerID
@Model.CustomerName
@Model.Phone
</div>
Very Important:
- A view can call the model. (with the support of controller)
- A view should be called by the controller.
- A view can't call the controller.
MVC Request Life Cycle :
View Engine :
- View Engine: It is the code syntax, that we have to use, while writing C# code in the views (pages).
Basic Example :
Step1: Open visual studio and select Asp.Net MVC application
Step 2:Select the application type that you needed that is Empty or internet or intranet..
Step3: Select the View Engine type that you are intended to develop
Step4: Creating Controller. Right click on controller folder and add Controller.
Step5: Adding a view.. Right click on the method that we want to create view and click “Add View”.
Step6:Adding Model… Right click on Model folder and add “class”.
Step7: Routing concept.. That to set Home page..
Step8: Set your Controller name and view name for Home page.
Action Results and Helper methods:
Conclusion
Hope this article helps for all MVC beginners.