Master pages of ASP. net mvc notes

Source: Internet
Author: User

Why use the master page? To unify the overall site style, any WEB application should use the master page. In the MVC framework, there is a new way to pass data to the master page.
A web application can contain multiple master pages, which are used to define the page layout. The biggest difference between a master page and a common page is that<Asp: ContentPlaceHolder>Label used to hold the content of a child page. When you create a view content page (a common page), you can select which master page to use. The content page will add<Asp: Content>Label to correspond to <asp: ContentPlaceHolder> on the master page. The [1] modify content page
When you use a master page, the title of the master page is used by default. You can customize the title in either of the following ways:
(1) Use the <% @ page %> label at the top of the page source file, such as <% @ page title = "Super Great Website"/>
(2) Sometimes you not only need to modify the Title, but also the content in the meta tag, you can include the Title and Meta tags in the <asp: contentPlaceHolder> inside the tag, you can use the <asp: Content> tag to redefine your Title and Meta when the view page needs to overwrite the Content.
It is defined as follows on a master page:

Code
<Head>
<Asp: ContentPlaceHolder ID = "head" runat = "server">
<Title> Please change my title </title>
<Meta name = "description" content = "Please provide a description"/>
<Meta name = "keywords" content = "keyword1, keyword2"/>
</Asp: ContentPlaceHolder>
</Head>

On the view page, you can use Content to overwrite the Content in ContentPlaceHolder:

Code
<Asp: Content ID = "Content1" ContentPlaceHolderID = "head" runat = "server">
<Title> The Index2 Page </title>
<Meta name = "description" content = "Description of Index2 page"/>
<Meta name = "keywords" content = "asp.net, mvc, cool, groovy"/>
</Asp: Content>

[2] transferring data
(1) Simple Method

UseViewDataThe data to be displayed is transmitted to the master page. ViewData is a Dictionary structure. The master page shares ViewData with the content page, as long as ViewData [key] is used, you can set or obtain the corresponding Value. You can iterate the ViewData list on the master page to display the data. However, there is a problem: You must assign values to ViewData in all the actions corresponding to the View that uses the master page, otherwise, the page displays an exception that "the object is not referenced to the object instance". To solve this problem, you have to add the same code to all actions and assign values to ViewData on the master page, however, this will lead to a reduction in the maintainability and adaptability of your program.
(2) better solutions

To solve the problem of repeated code and assign values to the master page at one time, you can define an image class to assign values to ViewData, while other Contrller classes inherit this image class. For example, define such a image class:

 

Code
Namespace MvcApplication1.Controllers
{
Public abstract class ApplicationController: Controller
{
Private MovieDataContext _ dataContext = new MovieDataContext ();

Public MovieDataContext DataContext
{
Get {return _ dataContext ;}
}

Public ApplicationController ()
{
ViewData ["categories"] = from c in DataContext. MovieCategories
Select c;
}

}
}

The constructor of this class implements data query and assigns values to ViewData. This ["categories"] is the key used in the master page. At the same time, this class inherits the Controller. Then the Contrller in other places can inherit this image class:

Public class MoviesController: ApplicationController

When the interface is displayed, because MoviesController inherits from ApplicationController, The ApplicationController constructor is called to query data, so that the data is displayed on the master page.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.