Monday, January 20, 2014

Step By Step How to create an MVC View

         By Carmel Schvartzman


In this tutorial we'll learn how to create an MVC View for an Action method  in ASP.NET MVC 4
An MVC View is the responsible for rendering the html for the UI with the user: it is the Presentation level in the ModelViewController software pattern; if so , it is absolutely forbidden to include in it any bussiness logic (wich is holded into the MVC Model) or any application logic (coded inside the MVC Controllers), nor any data fetching(MVC Model responsability, sent via the Controllers).

By convention, the Views are stored inside subfolders of the View folder, named after the corresponding Controllers: then for instance, all the Views you create related to the AccountController will be placed inside the "Account" folder ("Views/Account"). The MVC framework will search a required View to be rendered, in the folder with the name of the Controller, and if it is not there, will search at the "Shared" subfolder. If you want a View to be shared among several Controllers, place it in the "Shared" folder.

A View is just a class called ViewPage: it inherits from the Page class and also implements the IViewDataContainer, in order to hold all the data sent by the Controllers inside its ViewDataDictionary. There is another useful collection named TempDataDictionary, similar to the former, with the difference that it persists ONLY between the current request and the next request (unless you mark the key to be persisted more than that, using the Keep() method). This is useful while passing data between Action methods, or in case of errors.

To create your own View, first let's create a new Controller called "NewViewController", and then add a View to the Index Action method. So right click over the Controllers folder, and add a new Controller:


Name it "NewViewController", and select the template "Empty MVC Controller":


Open the Controller file and find the "Index" Action method:



Right click on it and select the option "Add View":


On the dialog, let the name be "Index", select the "ASPX" View engine, and choose a master page:


The new created View holds several Contents , areas that will be embedded inside the Master page:


Return to the Index Action method, and add a key with a message to the ViewDataDictionary:


Do the same with the TempDataDictionary:


We have just set the Controller to send 2 strings to the View. Now we have to receive the data in the View, so add the folowing code inside the Content2:


We're done. Let's see how it works. Start the application without debugging (CTL - F5):


Now append to the address , the name we give to the Controller, "NewView". The MVC framework will route the request to the "Index" Action method of the "NewView" controller:

And we'll get the following web page rendered with the sent data:



In this tutorial we've learned how to create an MVC View for an Action method  in ASP.NET MVC 4, sending data via the ViewDataDictionary and the TempDataDictionary
That's all!! 
Happy programming.....


כתב: כרמל שוורצמן