ASP.NET MVC 3: How to generate controls and views

Posted: March 22, 2012 in ASP.NET MVC3

I had been spending a great part of the night trying to figure out how could i automatic generate Controls and views in ASP.NET MVC 3 and i end up with a review to code first way to do things.

My problem was to allow some dynamic content to be manged directly from a website. For my example i used a canonical example based on books.

So here goes the review of automatic creation to the ones like me that don t remember sometimes this wonderful asp.net MVC 3 feature.

After long running R& D and tests i finally can do this. To do that i had to use some CRUD with scaffolding with Entity Framework and code first. So let’s meet my road to accomplish this.

Step1: Before sacaffolded, i create a model and a DbContext to define what data will be collected.  So i create the following classes

public class Book
{
public int ID { get; set; }
public string Title { get; set; }
public string Isbn { get; set; }
public string Summary { get; set; }
public string Author { get; set; }
public string Thumbnail { get; set; }
public double Price { get; set; }
public DateTime Published { get; set; }
}
public class BookDBContext : DbContext
{
public DbSet<Book> Books { get; set; }
}

The first class defines the concept of book and his data and actions. The second class is the context of book.

Step2: Create the model

i had use the right click of the mouse in to the Controller folder of my project and select Add Controller visual studio opens the window

This is a great way to quickly put your entities and elements running

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s