Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Adding Dynamic Data to an ASP.NET MVC5 website

0.00/5 (No votes)
18 Apr 2015 1  
ASP.NET Dynamic Data lets us manage data right on the website instead of having to access the database. You can add it as an admin interface to your MVC website, but it seems there has never been an easy way to do so. I'll try to make the task easier for you if you have never done it.
Dynamic Data UI
Dynamic Data UI

First, you would need to install the NuGet package Microsoft ASP.NET DynamicData EFProvider. The package will add some templates for generating the views based on your data context. But we will need more than that to make it work.

Dynamic Data - templates
The templates
DynamicData EFProvider package
DynamicData EFProvider package

Having the DynamicData folder from the package, now we need to create a new ASP.NET Dynamic Data Entities Web Application to get some files from it.

Creating new Dynamic Data web app

After creating the project, let's copy these files and folders to the DynamicData folder in our website:
Default.aspx, Default.aspx.cs, Site.master, Site.master.cs, Site.css, and Web.config; Content and Filters folders.

  • In Site.master
    • Change "~/Site.css" to "Site.css" (because the files are now in the same folder).
    • The href for "Back to home page" is for the homepage of our administration UI, let's change it to "~/dbadmin/home" (I use dbadmin/{pagename} for routeUrl, because using dbadmin only will mess up with the default route).
    • Add ~/ before DynamicData/Content/Images/back.gif.
  • Change "~/Site.master" to "Site.master" for Default.aspx.
  • Change "~/Site.master" to "..\Site.master" for all the views in PageTemplates.
  • Because ASP.NET Dynamic Data is WebForms, we need to install the package AspNet.ScriptManager.jQuery in order for the asp:ScriptManager controls to work.

Create a new class called Registration in your DynamicData folder. Change ApplicationDbContext to the data context you want to use for generating tables and fields for data management.

using System.Web.DynamicData;
using System.Web.Routing;
using DynamicDataSample.Models;
using Microsoft.AspNet.DynamicData.ModelProviders;

namespace DynamicDataSample.DynamicData
{
    public class Registration
    {
        public static readonly MetaModel DefaultModel = new MetaModel();

        public static void Register(RouteCollection routes)
        {
            DefaultModel.RegisterContext(
                new EFDataModelProvider(() => new ApplicationDbContext()),
                new ContextConfiguration { ScaffoldAllTables = true });

            // This route must come first to prevent some other route 
            // from the site to take over
            routes.Insert(
                0,
                new DynamicDataRoute("dbadmin/{table}/{action}")
                {
                    Constraints = new RouteValueDictionary
                                  (new { action = "List|Details|Edit|Insert" }),
                    Model = DefaultModel
                });

            routes.MapPageRoute(
                "dd_default",
                "dbadmin/{pagename}",
                "~/DynamicData/Default.aspx");
        }
    }
}

Here is how your DynamicData folder should be at this point:

Dynamic Data folder

Build the project, you will notice there's an error in Default.aspx.cs, we need to change the Global class to our new Registration class to fix:

Default.aspx.cs class

One more step: call the Register method for Dynamic Data in your Global.asax.cs, notice that it must be before the RegisterRoutes:

If everything is correct, you will see the Dynamic Data interface when you run the website and go to /dbadmin/home:

Dynamic Data homepage

For authorization, add something like this to the Site.master.cs:

protected override void OnInit(EventArgs e)
{
    if (!Page.User.Identity.IsAuthenticated)
    {
        Response.StatusCode = (int)HttpStatusCode.Unauthorized;
        Response.End();
    }

    if (!Page.User.IsInRole("Admin"))
    {
        Response.StatusCode = (int)HttpStatusCode.Forbidden;
        Response.End();
    }

    base.OnInit(e);
}

Reference

Professional ASP.NET MVC 4 is a great book, but it's not so great on Dynamic Data. I needed quite a few modifications, Googling around, and head-scratching... Hopefully, they will have some complete NuGet package for this in the future.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here