In this practice, you create a Dynamic Data website and edit the default routing.
oN the CoMpaNIoN MedIa
If you encounter a problem completing an exercise, you can find the completed projects in the samples installed from this book’s companion CD. For more information about the project files and other content on the CD, see “Using the Companion Media” in this book’s Introduction.
exercise Creating a Dynamic Data Website and an Entity Model
In this exercise, you create a new Dynamic Data website, add a database to the site, and define an Entity Data Model. You then configure the site to work with the model and customize the routing.
1. Open Visual Studio and create a new ASP.NET Dynamic Data Entities Web Site. Name the site DynamicDataLab. Select your preferred programming language.
2. Add the northwnd.mdf file to your App_Data directory. You can copy the file from the samples installed from the CD.
3. Add an ADO.NET Entity Data Model to your site. Name the model Northwind.edmx.
When prompted, allow Visual Studio to add this to your App_Code directory. Use the Entity Data Model Wizard to connect to all tables in the database; use the default settings throughout the wizard.
4. Open the Global.asax file. Inside RegisterRoutes, uncomment the line that calls RegisterContext. Change this code to register your new entity model and set ScaffoldAllTables to true. Your code should look as follows.
Sample of Visual Basic Code
‘VB
DefaultModel.RegisterContext(GetType(northwndModel.northwndEntities), _ New ContextConfiguration() With {.ScaffoldAllTables = True})
Sample of C# Code
DefaultModel.RegisterContext(typeof(northwndModel.northwndEntities), new ContextConfiguration() { ScaffoldAllTables = true });
5. Run the application and view the results. Select Products. Click to edit a product.
Notice the URL routing. On the product editing page, clear the name field and click update. Notice the validation that is displayed.
Lesson 3: Working with ASP.NET Dynamic Data CHAPTER 12 781 6. Return to the Global.asax file to enable routing to ListDetails.aspx. This page allows
you to edit a row directly in the GridView as well as in a DetailsView on the same page.
To enable row editing, comment out the routes.Add call for {table}/{action}. Then uncomment both routes.Add calls at the bottom of the method. Notice that these routes indicate that the List and Details actions should route to ListDetails.aspx. This page handles all other actions inline with the page.
7. Rerun the site. Select the Products table. Notice the new URL. Click the Edit link for a product in the GridView control. Notice the inline editing and data validation.
Lesson Summary
■ There are two Dynamic Data website templates in ASP.NET: one for working with the Entity Framework and one for working with LINQ to SQL models. Both create a set of page and field template files inside the DynamicData folder. Both allow you to connect your data context to the site inside the Global.asax file.
■ You can use the System.Web.Routing inside the Global.asax file to indicate how your Dynamic Data site maps entity requests and actions formatted in a URI to page templates.
■ You use partial classes to extend the metadata of your data context. This metadata includes attributes from the DataAnnotations namespace that define display format-ting properties and validation rules.
■ You can write additions to the OnChanging partial methods from your data context model to extend individual properties to include additional business logic.
■ You can create custom field templates that change how properties are displayed and edited. These field templates inherit from FieldTemplateUserControl. You apply a custom field template as metadata to a property in your partial entity class by using the UIHintAttribute.
■ You can create custom page templates that are entity specific inside the CustomPages folder. You add a folder with the same name as your entity. You then define custom action pages such as List.aspx and Edit.aspx.
■ You can use the EnableDynamicData method of the data view controls (such as GridView) to add Dynamic Data features to existing websites that do not contain the standard scaffolding.
Lesson Review
You can use the following questions to test your knowledge of the information in Lesson 3,
“Working with ASP.NET Dynamic Data.” The questions are also available on the companion CD in a practice test, if you prefer to review them in electronic form.
Note ANSWERS
Answers to these questions and explanations of why each answer choice is correct or incorrect are located in the “Answers” section at the end of the book.
1. You need to add metadata to your data context object, Invoice, in order to change how invoices are handled by Dynamic Data. Which actions should you take? (Choose all that apply.)
a. Create a new partial class called Invoice in the App_Code directory.
B. Create a new class called InvoiceAnnotations in the App_Code directory.
c. Decorate the Invoice class with the MetadataTypeAttribute class.
D. Decorate the InvoiceAnnotations class with the ScaffoldTableAttribute class.
2. You have defined a custom field template user control for changing the way an Invoice number is edited. You want to apply this control to the Invoice.Number property. Which data annotation attribute class would you use to do so?
a. MetadataType B. Display c. Editable D. UIHint
3. You want to implement custom business logic that should run when the InvoiceNumber property is modified. What actions should you take? (Choose all that apply.)
a. Add a CustomValidator control to the DynamicData/FieldTemplates/Integer_Edit.asax file. Set this control to process custom logic to validate an invoice number.
B. Extend the OnInvoiceNumberChanged partial method inside the Invoice partial class to include additional validation logic.
c. Extend the OnInvoiceNumberChanging partial method inside the Invoice partial class to include additional validation logic.
D. If the logic fails, throw a ValidationException instance.
Case Scenarios CHAPTER 12 783
Case Scenarios
In the following case scenarios, you apply what you’ve learned in this chapter. If you have difficulty completing this work, review the material in this chapter before beginning the next chapter. You can find answers to these questions in the “Answers” section at the end of this book.