Here I want to create a simple MVC website to store and view details of vehicles(Cars). Define the Work Flow as bellow.
1. Create a Database and a data Table to store information.
I have created a simple Cars table. [Usually table names are in plural, because it used singular form for the class name when Entity Framework automatically generates classes from the database ]
2. Visual Studio, create empty MVC 2 web application.
it generates a MVC template with View, Controller and Model
View – have all the UI views of the application. We normally don’t use visual studio supported server controls (buttons, lists etc..) to build the UI. so we use normal html controls to build up our UI.
[If you don’t like html, don’t worry there are some html Helper classes and with Razor, you can build a nice UI] , there are also some 3rd party UI tools.
Controllers – In this MVC model our URL request firstly hit to the Controller class the it starts communicate with view and model, and it has some URL routing mechanism. When we create a controller class, it names as “SOMENAMEController”, so then the accessing URL is like “http://localhost/SOMENAME”.
Controller class has methods. so we can invoke those methods by using “http://localhost/SOMENAME/methodname”. so this is the default URL routing mechanism, we can modify as we want.
Model – Where our business rules and db access layer is located. using Entity Framework we can obtain our data model very easily. [Here I use EFramework 4.1]
we can load our required data tables in to the model. We can apply inheritance rules can abstract rules for the classes. you can see our “Cars” Table –> Car Object.
select DbContextGenerator, if there is not available you can check @ online template section, download it, and use it.
then it automatically generates us some classes.
this is our Car Class, it has only properties and get,sets. I additionally use some validation rules.
Special thing is that you can see all the Generates classes and methods are “Partial”. so I think I don’t want to save what are the possibilities that “Partial” gives us.
3. Create Controller methods and Views
MVC template supports us to generate some Views for simple CRUD operations. the above image shows you, when you add a View to a particular action, it gives us a option window to chose the type of the View to be generated.
sample Action methods in the Controller Class.
No comments:
Post a Comment