15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

MVC ❘ 1265<br />

There is no code-behind file for this view; instead, it inherits the ASP.<strong>NET</strong> MVC generic base class<br />

ViewPage, located in the System.Web.Mvc namespace. The generic parameter used is, as you might have<br />

guessed, IEnumerable.<br />

This code is fairly self-explanatory, but some features are worthy of mention. First, note that the Web<br />

Form uses the site master page, <strong>and</strong> that the content regions are populated for you. Next, notice that<br />

the ASP.<strong>NET</strong> MVC framework has used the model type information to intelligently populate the view<br />

according to the properties available, including a looping structure to output information for multiple items.<br />

Finally, note that several helper methods are used to output model information <strong>and</strong> to add action links. An<br />

action link is a link that calls a controller method by navigating to an appropriate URL (more on this in the<br />

“URL Operation Routing” section).<br />

You are free to edit this view as you see fit. For example, in this application you don’t really need to expose<br />

the ProductId values, so you can remove the following sections:<br />

<strong>and</strong>:<br />

<br />

ProductId<br />

<br />

<br />

<br />

<br />

Don’t worry, this won’t break anything.<br />

Testing View Products functionality<br />

Now it’s time to test that things are working. Build the application <strong>and</strong> run it (enabling debugging when<br />

prompted). You haven’t yet added any code to provide a link to the new view, but that’s not a problem as all<br />

of the plumbing is in place. When you see the home page, manually edit the URL by adding Products to the<br />

end <strong>and</strong> navigate to the page. The display should be updated as shown in Figure 42-12.<br />

figure 42-12<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!