ASP.NET MVC 3 Scaffolding Quick Start
 
Published: 26 Apr 2011
Abstract
Using the latest ASP.NET MVC 3 tools, it's very quick to go from simple C# classes to working data management screens with a generated backing database. This article walks through the basics of getting started with this approach.
by Steven Smith
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 21642/ 29

Introduction

One of the new features in ASP.NET MVC 3 is its scaffolding feature, which basically lets you very quickly produce data entry controllers and views based on a particular model, even if you don't yet have a database in place.  This can let you very quickly produce a proof-of-concept, or iterate through some design options, without the need to go through a big process of setting up a database, tables, stored procedures, etc. just to show the customer how the UI might work with a given set of data.  To use this feature, you'll want to get the latest ASP.NET MVC 3 tools.

Scaffolding a Model Class

We'll start with a simple model class that intentionally uses a few different data types:

It's important that we annotate a property as being a [Key] in order for the scaffolding to work properly.  We can also specify many other metadata properties using attributes from the System.ComponentModel.DataAnnotations namespace, but for now we'll stick with just the [Key].

Now that we have this class, the next step is to add a Controller for this class by right-clicking on the Controllers folder in Solution Explorer:

Fill in the resulting window with the name of the model class, the template to use (I'm using the default Entity Framework read/write template), and the data context class.  Since I'm starting from a brand new project, I don't have a data context class yet, so I selected <New data context> from the list of options, and named the class BookContext.

Once you click Add, you should see a number of new items added to your project:

More interestingly, if you simply run your web application at this point and navigate to /book, you will see that you now have a working Controller and View for managing a collection of books:

Click Create New, fill in the form, and you'll see you now have full CRUD-L (Create, Read, Update, Delete -- and List) access to your model class.

Note that the display name for PublicationDate isn't necessarily how the user would expect.  We can fix that with some annotations on the Create/Edit views, though on the Index view for now I'm just fixing it manually.  Adding the attribute [Display] to the property allows us to specify a Name to show:

This is what's used by the views that use Html.LabelFor(), like this line in the Edit.cshtml View:

Note that if you change your model, you're likely to get an error regarding the database being out of sync with your model.  Looking at the generated code for the BookContext, we see this comment which has the line of code needed to fix this issue, assuming you are ok with deleting your database on every application start!

Once this is added to Global.asax:

The error disappears and we're able to continue working on our model, its annotations, and the look and feel of our scaffolded CRUD views and controllers.

 

Summary

The latest tools for ASP.NET MVC 3 make it extremely easy to get started with a data driven application starting with code.  You can quickly create a model in C#, provide a few simple annotations to specify key values, display names, and validation constraints, and then simple but functional data management screens are a few clicks away.  The result, while easy to produce, is generally good code (though it would be a better design not to call the data context directly from the controller, but rather to inject it via an IRepository interface) that can easily be tailored to suit production requirements.

Download the sample code for this article here.

 



User Comments

Title: Head   
Name: Rashmi
Date: 2011-05-16 12:02:42 AM
Comment:
this is a great article. Will help me in my project.
Title: insert problem   
Name: Eile
Date: 2011-05-13 7:39:07 PM
Comment:
if i don't want to insert the publication Date what should i do? Or set the publication Date to Datatime.now()

it doesn't looks it will work if i delete publication date code in Create.cshtml page. There should be an opinion on what you want field that you want insert and what you dont
Title: One model per controller?   
Name: Sam
Date: 2011-05-12 8:57:00 PM
Comment:
Who uses only one model per controller? I tried it and it doesn't work in practice. I have a model for every unique form which typically results in an add model, edit model, view model, listing model and search model (for the listing search form). This tooling would be useful if it prompted for the model class to use for each of operation the controller 'scaffolds' for you.
Title: Thanks   
Name: Me
Date: 2011-05-12 8:31:09 AM
Comment:
Short and sweet. Nice.
Title: Very good article   
Name: Kaka
Date: 2011-05-12 5:07:26 AM
Comment:
nice






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-03-29 1:04:42 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search