Sunday 2 August 2015

ASP.NET Views


  • When you return a ViewResult from an action method, it indicates that ASP.NET MVC should invoke a view page to render HTML back to the client browser.
  • You store view pages in the Views directory inside the application. 
  • Views often include server-side code to produce the final HTML. 


Check boxes in the Add View dialog offer three additional view characteristics that you can specify:


   Create a strongly-typed view: Check this box if you want to make your view a strongly typed view, which is a view that receives a known type of “model” object as a parameter from the controller action method. Using strongly typed views has several advantages, such as:
  • IntelliSense: Visual Studio will be capable of displaying IntelliSense using the Model property based on the view model class. This allows the view page to access properties on the model object, call methods, and so forth.
  • Automatic scaffolding: Selecting this option instructs Visual Studio to create a view with a tag that includes all the fields and validation options based on the properties defined in the view model class
  • Compile-time type checking: The compiler is able to detect problems with data type handling in the view because it knows the view model class and its properties. For example, if the view page tries to access a property that doesn’t exist on the model object, you’ll get a compiler error rather than a runtime error. This is a good thing, because it helps you fix problems in your code more easily


    Create as a partial view: Check this box to define the view as a partial view, which is a chunk of HTML that can be reused in other views. For example, a partial view can contain the logic to display a chart based on a set of data. This chart then can be placed in different views, but the logic remains centralized in a single partial view. This means that if you modify the chart type, for example, from a bar chart to a pie chart, all views having the same chart will be displayed as a pie chart.

     Use a layout or master page: Checking this box allows you to define a view that serves as a general layout with elements such as a logo, a top menu, and a footer that will be the same across all views that use it. This is a good way to give your web site a consistent look and feel. Also, changes that you make in this shared view apply to all views that use it, which is much easier than having to make the changes in each individual view. In Razor, there is a default layout view called _ViewStart.cshtml (or _ViewStart.vbhtml if your server-side language is VB.NET). The _ViewStart.cshtml view is used if you selected the check box “Use a layout or master page” and left the layout input box empty.

No comments:

Post a Comment