15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

1262 ❘ ChaPTer 42 Asp.net dynAmic dAtA And mvc<br />

At this point, you can run the application <strong>and</strong> see what’s there. There’s a perfectly functional home page<br />

with an About page <strong>and</strong> a registration/log on system. However, it’s easier to learn about how everything<br />

works if you work through modifying what is there.<br />

adding a Model<br />

The order in which you add model, view, <strong>and</strong> controller code will vary according to what you want to<br />

achieve. Typically, though, you will start with a model, particularly if you want to access data in a database.<br />

By starting with a model, you then have access to strongly typed model classes from the outset, <strong>and</strong> the ASP<br />

.<strong>NET</strong> MVC wizards will be able to use the model type information to automatically generate a lot of the<br />

scaffolding code you require.<br />

In this example, you will use the MagicShop.mdf database introduced earlier in the chapter. Because the<br />

sample code is a web application rather than a web site, you cannot simply copy this file to the App_Data<br />

directory. Instead, you must add it to the App_Data directory by right-clicking on the directory <strong>and</strong> selecting<br />

the Add ➪ Existing Item . . . menu item, then browsing to the database file <strong>and</strong> clicking Add. Or, if you prefer,<br />

you can build a database in place through Visual Studio, connect to a different database, or select an entirely<br />

different data source.<br />

Once you have a data source in place, you can add a model using whatever technique you prefer. This<br />

includes using LINQ to SQL classes, an ADO.<strong>NET</strong> Entity Data Model, or custom classes that you design<br />

from scratch. Whichever method you use, you should place the code in the Models folder of the application.<br />

For simplicity, add an ADO.<strong>NET</strong> Entity Data Model with the name MagicShop by right-clicking on the<br />

Models directory <strong>and</strong> selecting the Add ➪ New Item . . . menu item. This template is located in the Data<br />

templates section. Since a data source exists, most of the default settings in the wizard will already be<br />

configured for you, so all you have to do is to select the tables to model (all of them).<br />

After adding your model code, you must build the solution before proceeding in order to make the<br />

subsequent wizards aware of your new code.<br />

adding a Controller<br />

To add a controller for your new model, you use the ASP<br />

.<strong>NET</strong> MVC Add Controller Wizard. You access this<br />

wizard by right-clicking on the Controllers folder <strong>and</strong><br />

selecting the Add New Controller menu item.<br />

Add a controller for the products in the MagicShop database<br />

with the name ProductsController, as shown in Figure 42-9,<br />

ensuring that the check box is checked as shown.<br />

figure 42-9<br />

The controller that is added contains several methods, each of which returns an ActionResult.<br />

These methods encapsulate operations that users (or other code) can perform, <strong>and</strong> each is exposed<br />

through a URL through routing (see the section “URL Operation Routing” for more details). The<br />

methods added by the wizard are shown in the following table.<br />

meThod<br />

Index()<br />

Create()<br />

Details()<br />

Edit()<br />

desCriPTion<br />

This is the default operation for the controller, which views will use if no other method is<br />

specified; for example, when a view is first displayed. For the products controller, you want<br />

this to display a list of products.<br />

There are two methods added for creating new items. The first, which takes no parameters,<br />

is used when a request to add a new item is triggered, perhaps with an “Add new item”<br />

link. The second is responsible for actually adding an item.<br />

This method is responsible for displaying a detail view of a single item.<br />

The two edit methods are similar to the two create methods. The first takes a single ID<br />

value <strong>and</strong> will trigger editing for a specific, existing item. The second takes an ID <strong>and</strong> a<br />

second parameter that specifies the values to change.<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!