Walkthrough: Editing Data From a Domain Service
In this walkthrough, you learn how to create an interface that enables the user to change the displayed data and to save those changes in the databases.
When you have added update, insert, or delete methods in a domain service, you can create an interface in a Silverlight client that enables users to change the data. All of the changes are tracked in an EntityChangeSet object, and the changes are submitted collectively when you call the SubmitChanges method.
This and the other walkthroughs presented in the WCF RIA Services documentation require several prerequisite programs, such as Visual Studio 2010 and the Silverlight Developer Runtime and SDK, be installed and configured properly, in addition to WCF RIA Services and the WCF RIA Services Toolkit. They also require installing and configuring SQL Server 2008 R2 Express with Advanced Services and installing the AdventureWorks OLTP and LT database.
Detailed instructions for the satisfaction of each of these prerequisites are provided by the topics within the Prerequisites for WCF RIA Services node. Follow the instructions provided there before proceeding with this walkthrough to ensure that you encounter as few problems as possible when working through this RIA Services walkthroughs.
This walkthrough assumes that you have completed the Walkthrough: Creating a RIA Services Solution and continues from the application created with the procedures described there.
Open the RIAServicesExample solution from Walkthrough: Creating a RIA Services Solution.
In MainPage.xaml, change the interface to enable the user to save or reject changes in the DataGrid.
The following XAML adds a Save Changes button, a Reject Changes button, and a text block.
In the code-behind page for MainPage.xaml, add event handlers for the button click events, an event handler for the RowEditEnded event, a callback method named
OnSubmitCompleted, and a method that evaluates the pending changes.
In the server project, open the metadata class named Customer.metadata.cs or Customer.metadata.vb.
For more information, see How to: Add Metadata Classes.
In the metadata class, add the EditableAttribute attribute with the AllowEdit property set to false to the
You apply the EditableAttribute attribute to a property to indicate whether the property is intended for editing by a user in a client application. When you set the AllowEdit property to false, the property is read-only in the client application. In this example, you will display the values for the
ModifiedDateproperties but do not want the user to modify these values.
Add the ExcludeAttribute attribute to the
You apply the ExcludeAttribute attribute to properties that you do not want to include in the generated code for the client project. In this example, there is no reason to expose the
rowguidproperty in the client project.
The following shows the contents of the metadata class.
Run (F5) the application.
Notice that you can edit the values in the DataGrid. When you exit the row you have edited, the
ChangeTextvalue is updated with a description of the pending changes. When you click the Save Changes button, the data changes are saved in the database. When you click the Reject Changes button, all pending changes are reverted.