260Microsoft Visual Studio 2010: A Beginner’s Guide

Moving from the top of Listing 9-3 down, you can see the MasterPage directive at the top of the page, which states that this is a MasterPage and ASP.NET MVC will handle the page appropriately. The DTD is a tag that specifies what Web standards this page supports, which is read by browsers to help them determine the best way to display the page.

The rest of the page is framed inside of HTML tags and ASP.NET MVC markup. The html tag states that this is an HTML document. HTML documents have two parts, a head and a body, where the head is for metadata describing the page and the body contains display content.

In HTML, a div tag blocks off a chunk of HTML and is useful for layout and organization of the page. The Hx tags, where x is a number between 1 and 6, describe headers, where h1 is the largest and h6 is the smallest.

The ContentPlaceHolder controls are instrumental to the success of the MasterPage. If you look at the Content tags in Listing 9-2, you’ll see that they have a ContentPlaceHolderID that matches the ID attributes of the ContentPlaceHolder controls in Listing 9-3. What this means is that when the View renders, the MasterPage will display and ASP.NET MVC will inject the Content regions of the content pages into the matching ContentPlaceHolders of the MasterPage. ASP.NET MVC knows which MasterPage to use because the Page directive, as shown in Listing 9-2, specifies the MasterPage attribute.

If you recall from the last section, Listing 9-2 had a binding expression for the Html Encode helper method. The MasterPage in Listing 9-3 introduces a couple more Html helper methods, RenderPartial and ActionLink.

The ActionLink method has three parameters: id, controller, and action. When the ActionLink renders in the browser, it will transform into an anchor tag, a, with an id specified in the first parameter of ActionLink. When the user clicks the link in the browser, the application will navigate to the Controller in the third parameter of ActionLink and invoke the action in the second parameter of ActionLink. So, if the user clicked the link produced by ActionLink("About", "About", "Home"), ASP.NET MVC will invoke the About action of the Home Controller. The next section discusses RenderPartial in more detail.

Partial Views (a.k.a. User Controls)

It’s often the case that you’ve written View content on one page and need the same identical content on two or more pages. As explained with MasterPages, you want to avoid the maintenance work that comes with updating all of the content that is the same on multiple pages. While MasterPages are good for content that decorates pages across an entire site, a Partial View is ideal for limited reuse of View content on different pages of a site.

Page 283
Image 283
Microsoft 9GD00001 manual Partial Views a.k.a. User Controls