Click here to Skip to main content
15,887,328 members
Articles / Web Development / ASP.NET

Adding Dynamic Data to an ASP.NET MVC5 website

Rate me:
Please Sign up or sign in to vote.
4.81/5 (9 votes)
18 Apr 2015CPOL2 min read 25.9K   16   3
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.

C#
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:

Image 7

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:

C#
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.

This article was originally posted at http://x189.blogspot.com

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Vietnam Vietnam
ASP.NET MVC enthusiast.
Developer @ X189 blog.

Comments and Discussions

 
QuestionThanks, that helped me a lot Pin
Member 125143681-Sep-16 13:56
Member 125143681-Sep-16 13:56 
QuestionAsp.net dynamic data to MVC application Pin
Member 102107566-Jun-16 21:21
Member 102107566-Jun-16 21:21 
SuggestionGot me to where I wanted to be. 3 stars Pin
Jordon Kraft6-May-15 8:34
Jordon Kraft6-May-15 8:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.