Friday, April 17, 2009

Adding DynamicData to an ASP.NET MVC application

In my previous post I've pointed you to a place that contains a ready-to-use solution on how to use DynamicData with ASP.NET MVC. In this post I'll describe the manual way of doing that so that you can see that there's no mystery hiding behind the scenes.

I'm going to assume that you have an up-and-running project created using ASP.NET MVC Application template.

Step 1: Create a separate DynamicData project. Call it whatever you want - we're going to need a few files from that project.

Step 2: Copy the following files from the DynamicData project right into the MVC application and include them in the MVC project:

  • /Site.css

  • /Site.master

  • /Site.master.cs

  • /Site.master.designer.cs

  • /DynamicData (the whole folder!)


Step 3: We need to register the DynamicData engine in our application. To do that copy the following lines from the Web.config file in DynamicData project into the Web.config file in ASP.NET MVC project:

  • from \\configuration\system.web\compilation\assemblies
    <add assembly="System.Web.DynamicData, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

  • from \\configuration\system.web\pages\controls
    <add tagPrefix="asp" namespace="System.Web.DynamicData" assembly="System.Web.DynamicData, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>

Step 4: Add a reference to System.Web.DynamicData to the MVC project.

Step 5: Register routes in Global.asax.cs. Depending on the context you'll have to adjust the context registration. Here I'm using the Northwind data context:

MetaModel model = new MetaModel();
model.RegisterContext(
typeof(MvcApplication1.Models.NorthwindDataContext),
new ContextConfiguration() {
ScaffoldAllTables = true
});
routes.Add(
new DynamicDataRoute("DD/{table}/{action}.aspx") {
Constraints = new RouteValueDictionary(
new { action = "List|Details|Edit|Insert" }),
Model = model
});

Make sure you add the proper using statement to System.Web.DynamicData and your models!

Step 6: Add a listing of all the tables to your Views/Home/Index.aspx:

<% foreach (MetaTable table in MetaModel.Default.Tables) { %>
<li>
<a href="<% =table.GetActionPath("List") %>">
<% =table.DisplayName %>
</a>
</li>
<% } %>


Done!

Run your application. On the initial page you should see a clickable list of all the tables from your database.

Enjoy!

4 comments:

Unknown said...

Hi there. I know this post is eons old but I as trying to get this to work with the latest ASP.Net MVC. My links all look like this:
http://localhost:51723/Home/List?Table=AssignmentSet

I've messed with the URl to no avail in an attempt to maybe find the scaffolds even if the routing set up is wrong, but no dice. Any help you could lend would be greatly appreciated. Thanks for the article...

Matthias Hryniszak said...

Do you have some test application that you can share so that I can see the problem on my own? I could then reproduce the issue and help you out with it.

Gabriel Fuller said...

This helped me a ton! Thanks! Works like a charm!

Gabriel Fuller said...

This helped me a ton! Thanks! Works like a charm!