Follow by Email

Thursday, April 25, 2013

MVC Demo using razor engine


MVC:
Model : All the operations related to fetching of data is handled here and also application state is maintained.
View: It the User Interface of an application.
Controller: All the web request are first accepted by the controller and also it acts as a intermediate to view and model. Controller decides what action has to be taken for all web requests.

Now lets create a sample MVC application:
1. Go to file –> New --> project –> select asp.net MVC 4 web application and click ok.
1


2. Enter name for your application. Select an Empty Project and then select view engine as razor. uncheck the create unit test project and then click ok.
3. Then after this go to solution explorer. Right click on controller and add controller. The name of the controller should be always anynameController, as in my case its HomeController.
Click on add and the HomeController.cs will get added to the controller folder of the application.

5. Now in models folder add one class file like in my case its Model1.cs and write this below code inside the class.
6. Again going back to the controller , change the namespace to as of models so that we can access properties of model class. Then describe method implementation for method Index(). After this build the application(This is compulsory) . Then Right click on index() and add view.
 7. This below window will get appear. The view name should be same as of the name of method index() in the controller. Select View engine as Razor and select create a strongly typed view and from drop-down select model class, then add.

8. In view we will display the empid and empname to the user whose value has been set in index() method inside the controller. As we are using RAZOR engine , @ symbol is used to describe a razor syntax.

 9. Now run the application it should the below output as shown below:
Protected by Copyscape Duplicate Content Detection Tool