ASP.NET MVC 3 Scaffolding Quick Start
page 2 of 3
by Steven Smith
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 21383/ 57

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.

 


View Entire Article

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-05-18 7:37:51 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search