Tuesday, December 21, 2010

ASP.NET MVC Views

Views - Introduction

  • The View is responsible for providing the user interface (UI) to the user.
  • Views should not contain application and business logic.
  • View Engine is used to generate views. The default view engine is WebFormViewEngine.
  • Views in ASP.NET MVC derive from a common base class, System.Web.Mvc.ViewPage, which itself derives from System.Web.UI.Page. Strongly typed Views derive from the generic ViewPage<T>.
  • View is able to take advantage of Master Pages, one of the benefits of building on Views on top of the existing ASP.NET Page infrastructure.
  • ASP.NET MVC provides a library of helper methods used to output properly encoded HTML, which provides a lot of help in creating Views.
  • A view renders the appropriate UI by using the data that is passed to it from the controller. This data is passed to a view from a controller action method by using the View method.

View Structure

  • When we create a new ASP.NET MVC application, the project contains “Views” directory structured in a very specific way. See the following figure

                                  
  • The Views directory contains a folder per Controller, with the same name as the Controller, sans the “Controller” suffix. Within each Controller folder, there’s a View file for each action method, named the same as the action method.
  • The action method can return a ViewResult via the View method like,

public ActionResult Index()
{
ViewData["Message"] = "Welcome to Balamurugan Nattarasan Blog!";
                   return View();
          }


  • If the View name is not specified in the ViewResult then the View name same as the action name in the /Views/ControllerName directory.
  • In the above example, the View would be /Views/Home/Index.aspx.
  • The View method calls the view engine, which uses the data in the list to render to the view and to display it in the browser.
  • The View can be overridden. You want the Index action render a different then you could pass different View name like,

public ActionResult Index()
{
ViewData["Message"] = "Welcome to Balamurugan Nattarasan Blog!";
                   return View("TestMethod");
          }

  • You want to specify the View in a completely different directory structure, then you can use tilde (~) symbol to provide the full path to the View like,

public ActionResult Index()
{
ViewData["Message"] = "Welcome to Balamurugan Nattarasan Blog!";
                   return View("~/Test/TestMethod1.aspx");
          }

  • When using the tilde syntax, you must supply the file extension of the View because this bypasses the View engine’s internal lookup mechanism for finding Views.


View Pages
·         A view page is an instance of the ViewPage class. It inherits from the Page class and implements the IViewDataContainer interface.
·         The ViewPage class defines a ViewData property that returns a ViewDataDictionary object. This property contains the data that the view should display.
·         The MVC framework uses URL routing to determine which controller action to invoke, and the controller action then decides which views to render.

Master-Page Views
·         ASP.NET page views (.aspx files) can use master pages to define a consistent layout and structure. In a typical site, the master page is bound to a content page in the @ Page directive of the content page.
·         You can also use dynamic master pages (that is, you can assign a master page at run time) when you call the View method of the Controller class.

Partial Views
·         A partial view enables you to define a view that will be rendered inside a parent view. Partial views are implemented as ASP.NET user controls (.ascx).
·         When a partial view is instantiated, it gets its own copy of the ViewDataDictionary object that is available to the parent view. The partial view therefore has access to the data of the parent view.
·         However, if the partial view updates the data, those updates affect only the partial view's ViewData object. The parent view's data is not changed.

Shared Views
·         The ASP.NET MVC framework always attempts to retrieve a view from the Shared folder when the view can’t be retrieved from the Views subfolder that corresponds to the controller’s name. This means that you can create default views for action methods that are shared across multiple controllers.
·         For example HomeController exposes an action named Index(). When the Index() action is invoked, the ASP.NET MVC framework first attempts to retrieve the Index view from the following path:
\Views\Home\Index.aspx
·         If the Index.aspx view is not present in the Home folder, the ASP.NET MVC framework next attempts to retrieve the view from the Shared folder:
\Views\Shared\Index.aspx
·         If the Index.aspx view can’t be retrieved from either location, then the error message will be displayed.