Dynamic Data with ASP.NET

Dheeraj Kumar Gunti

Reading Time : ( words)

ASP.NET Dynamic Data provides a framework that enables you to quickly build a functional data-driven application, based on a LINQ to SQL or Entity Framework data model. Rapid Action Development is significantly increased by the use of a rich scaffolding framework. After you add a LINQ to SQL or Entity Framework data model to a project, you can simply register it with Dynamic Data. The result is a fully functional Web site. Full CRUD (create, read, update, and delete) operations are supported. The site includes filtering by foreign keys and Boolean fields; foreign keys are automatically converted to their friendly names. Smart validation is automatically available, which provides validation based on database constraints for nullable fields, data type, and field length.
Dynamic Data will then automatically build the UI for these controls based on the templates that you specify.
Validation is significantly improved in the controls as well. The controls read metadata for a LINQ to SQL or Entity Framework data model and provide automatic validation based on the model. For example, if a column in the database is limited to 50 characters, and if a column is marked as not nullable, a RequiredFieldValidator control is automatically enabled for the column.

  • Dynamic Data offers the following features:
  • Support for multiple data models and data validation.
  • Support for table relationships. Dynamic Data detects relationships between tables and creates a UI that enables the user to view data from related tables. Dynamic Data supports displaying and editing data in a many-to-many relationship.
  • Support for customizing data-field validation.
  • Support for data filtering.
  • Support for table action links. Dynamic Data allows for the creation of dynamic hyperlinks that are based on the table actions enabled by Dynamic Data.
Following is the Sample Example for creation of Dynamic Data web application.
Create a new Dynamic Data Web Application.

Following will be the structure you see once you create a Dynamic Data web application. A Folder with Dynamic Data will be created under the solution.

Once you look in to the Dynamic Data folder you see,

  • Field Templates
  • Page Templates
Page Templates holds the default structure of the pages like, Details, Add, Edit etc.
Field Templates holds when ever add or edit records are done in pages automatically respective user controls for the field level controls
will be shown with respect to field validations.

- Create a Database.
- Create tables with the following structure(s).

We defined "SID" column for Foreign Key specification which we are going to bind up using database diagrams. There is a Column level computation specification is given for "TOTAL" field for calculating total automatically.

In the Database Explorer, Add new Diagram for creating a Database Diagram.

There will be a confirmation shown for creating database diagram. Click Yes to create a database diagram.

Select the tables for which you wish to create database diagram.

Drag ID column in Student table to SID column in Marks table for forming up foreign key relationship.

Once the foreign key is established it will ask for updating the schema in the database. Click Yes to update the schema.
Note: If Visual Studio doesn't allow you to update the schema then Go To Tools > Options > Database Tools > Table and Database Designers > Prevent saving changes that require table re-creation

Add New Item (LINQ to SQL Classes) to solution.

On the designer drag the selected tables to from up context.

Once the tables are dragged and dropped in to the designer automatically the relationships also be shown as below.

Open global.asax file which is there in the solution.

There is a piece of code, which illustrates the Context Registration to the application. Uncomment the selected line below.

In the "YourDataContextType" specify the context of the application which we can found in the designer of .dbml file

Following is the DataContext (ExampleModelDataContext) of the current application.

Specify the Context as below.

Make ScaffoldAllTables element to true. It will be false by default. ScaffoldAllTables option is used for having automatic generation of operations page level.
Click Here to know more about ScaffoldAllTables

Once all the settings are done. Run the application. You will see the tables list as below.

Click on Students link you will see the details about that table. Click on Insert new Item to create a student record.

Following will be the screen shown for adding the record.

Once the record is added you will see View Marks link. Since the tables Student and Marks are having relationship.

Add new item to Marks table by using Insert New Item.

You will see the Student record relation which you see in the following image.

There is one more in built functionality in Dynamic Data Web Application which is "Routing"
Open the Global.asax file Routing will be registered and specification can be done in different ways.

We can modify the routing link as below in the global.asax.

The modified link will be as below.