Posted By : Murugan Andezuthu Dharmaratnam
Posted On : 22 January 2015
Keywords : Views, MVC VIews, ASP .Net MVC VIews, ASP .Net, MVC

MVC View


MVC is used for user interface implementation.MVC stands for ModelViewController,actaually it is a software pattern interconnected with different parts.Due to this interconnection we can seperate the representation of information as they arrived.


View In MVC


View in MVC transforms a single model or set of models into a visual representation.Views should concentrate on displaying data and should not contain any business logic,the business logic stays in the model, which should provide the view.


The View contains HTML files to display output of the application (the user interfaces),depending on the language these files may have the extensions html, asp, aspx, cshtml, and vbhtml.


To Pass Data From Controller to the view you can use:
1. ViewData: ViewData is a property of ControllerBase class, Its life is limited to the current request.
2. ViewBag: Similar to ViewData, ViewBag is a dynamic property that takes advantage of the new dynamic features in C# 4.0.
3. TempData: TempData is used to pass data from current request to subsequent request, to target,life is till the target view is fully loaded, is a dictionary object derived from TempDataDictionary.

Controller needs to display HTML to the user,for that it returns an instance of VIEwRwsult,this helps to render content of response.During the rendering time of View MVC framework look for the view using the same name provided by the controller. Take the Index action in the HomeController:


Here View() method helps to create a ViewResult.This action takes advantage of the View() helper method to create a ViewResult. Calling View() without any parameters, helps to find a view with the same name as the current controller action.

Razor Syntax


Razor syntax is a template markup syntax that lets you embed server Code into web pages, it allows you to create dynamic pages in .Net. Razor syntax starts code blocks with a @ character and You need not explicitly close of the code-block.File extension for view page is .cshtml for Razor with C# or .vbhtml for Razor with VB.

Razor Basics


1. @ is equivalent to response.write();
2. @* *@ Comment
3. @{
ode Blocks
}
4. @Total Implicit
5. @(Total/10) Explicit
6. sample@pixelstroke.com email is Not evaluated as C# Code. Razor is smart
7. @@ - Escapes @Sign or displays @
8. @Section MySection{
}
@RenderSection("MySection", true)
9. Automatically encodes to prevent against cross site scripting attack.
10. @Html.Raw Returns markup that is not HTML encoded.

Working With Views


Working with view includes craeting or adding view,locating views etc.These are very easier procedure with simple steps.

Adding A View


For adding a view we need to change the controller class to use view template files.For that we need to create a view template file using theRazor view engine.The following steps explain the process.
Step 1: Create a template page in controller class using Index method.Change the Index method to return a View object

Step 2: For adding a view template using index method simply right click inside the method and select "Add View".
Step 3: A dialogue box appears,give a name and click on "Add" button.
Step 4: Now a view named "TestIndex" is created with .cshtml extension.Which is present in solution explorer.
Step 5: You can open the file by simply double clicking on it.
Step 6: You can add contents after the heading tag.Now I am going to include a sentence "This is our template page.".
Step 7: Inorder to see the output right click on the template file and select "View in Page Inspector".Now we get a webpage with our content.

Conclusion


This article about "MVC View" describes all basic things about view.Actually a view can be any output representation of information, such as a chart or a diagram; multiple views of the same information are possible, like a bar chart.


Murugan Andezuthu Dharmaratnam

Murugan is a technology leader with 14 years of hands-on experience in full life cycle systems design, development, implementation, and management. In addition to systems design and development, his core competencies include cloud design and architecture, interactive web applications and websites development, mobile computing, and Healthcare Information Technology. His experience is very diverse with a broad range of technologies within multiple industry settings including private, public, and non-profit. Coupled with his technology expertise, Murugan is a well-organized, result-oriented, and a highly analytical leader with proven track record in developing and implementing very complex systems.