ASP.NET MVC3 with MvcScaffolding

by Aref Mon, July 04 2011 18:57

Do you find yourself repeating the same set of arduous and potentially error-introducing tasks each time you sprint or create a new project? Do you work with databases and typically write code to link up the data model to EF/Code and then do additional plumbing for managing entity relationships in your views and controllers? What about consistent validation across the EF code first to the controller and the views?

This article assumes that you have installed the latest MVC 3 bits into your development environment. I am using Visual Studio 2010 Ultimate. If you haven’t installed the latest MVC 3, you should do that first by going here: http://bit.ly/jnsOR0

In an over-simplification, MvcScaffolding is to your web application, what Code Snippets is to your document (code, xml, etc.). In other words, using MvcScaffolding you can, for instance, automatically generate

1.       Database contexts from your model,

2.       Controllers for creating, viewing, updating and deleting instances of your model or your viewmodel,

3.       Corresponding Views for creating, viewing, updating, and deleting data,

4.       Catalyze the generation of a database model including support for one-to-many relationships and more…

In addition, you can customize your own scaffolding for instance to provide for default Localization support, custom authentication/authorization support, etc.

In this article we will walk through the creation of a read/write/update/delete of Recipes and its related (many) Ingredients using MvcScaffolding and MVC 3.

Start your engines…

(often-times, the launch of Visual Studio seems like it given the time that it takes)

Step 1. Ensure that your development environment (again, I am using Visual Studio) has the latest MVC 3 bits installed. See above if you haven’t done so already.

Step 2. Create a new MVC 3 Project

         

Choose the ASP.NET MVC3 Web Application Template as shown above,

       

 

I selected the Intranet Application option, you may use Internet or Intranet. Make sure that you are using the Razor View Engine and continue by clicking on OK.

Step 3. Deploy the latest MvcScaffolding

Now you are ready to deploy the latest MvcScaffolding package to your solution. To do this,

First launch the Package Manager Console

         

You will see the console with a PM> prompt. Type the following as shown below

                PM> Install-Package MvcScaffolding

         

You should then get the following results (you must be connected to the internet in order for this to work).

         

Step 4. Create your models

Here we are using Entity Framework 4’s code-first magic to first create classes for our entities.

Let us start by creating a class for Order (many side of the one-to-many relationship).

         

Let us now create a class for Customer (one side of the one-to-many relationship).

         

You will notice that we have included a foreign key for Customer in the Order class. Also, we have included a virtual collection of Orders in the Customer class to represent the orders placed by that customer and likewise, we have placed a virtual Customer member in the Order class to represent the customer for that order. You will see shortly how this plays out.

Step 5. Scaffold your models

Now the magic begins. For each of your models (Order, Customer) use the Package Manager Console to scaffold out the database plumbing, controllers and views.

Let us start by scaffolding the Order model by running the following

                PM> Scaffold Controller Order –For ce

         

Now do the same for the Customer model.

Step 6. Compile and run your application

…the moment of truth. Run your application and browse to the Customers controller default view as shown below

         

Create a few customers (notice how the validation is working)

         

Now create a couple of orders for one of your customers. (Notice how the drop down brings in the customer names to choose for the order. Nice eh?)

         

         

Now list your orders (Notice how it shows the customer name for each order)

         

List the customers (Notice how it shows the total number of orders for each customer)

         

Browse away and enjoy.

Notes.

I hope that you have found this useful. Scaffolding is a powerful concept to quickly build out functionality to your MVC applications. Maybe you can customize the scaffolding to include internationalization/localization support and share it back with us? I have attached the source code for this project.

Downloads


Download Source Code (2.68 mb)

 

Download as PDF (941.45 kb)

Tags: , , ,

.NET MVC

Comments

7/4/2011 11:23:52 PM #

Aref Moin

To learn more about scaffolding, visit Steve Sanderson's blog at blog.stevensanderson.com/.../

Aref Moin Canada |

7/4/2011 11:38:52 PM #

trackback

ASP.NET MVC3 with MvcScaffolding

You've been kicked (a good thing) - Trackback from DotNetKicks.com

DotNetKicks.com |

7/6/2011 9:52:29 AM #

Cute Love Quotes

Good post. I be taught something more challenging on different blogs everyday. It'll all the time be stimulating to read content material from other writers and follow just a little something from their store. I’d prefer to make use of some with the content on my blog whether you don’t mind. Natually I’ll give you a link on your net blog. Thanks for sharing.

Cute Love Quotes United States |

7/28/2011 8:33:56 AM #

sjr luxuria

Hello! I just would like to give a huge thumbs up for the great info you have here on this post. I will be coming back to your blog for more soon.

sjr luxuria United States |

8/6/2011 4:14:42 AM #

 hosts101.com

hosts101.com is a reliable and cheap web hosting company.I have been usinng their business plane since last  3 years.Unlimited hosting with a free Domain not bad.
http://www.hosts101.com/

hosts101.com Bangladesh |

8/9/2011 2:17:42 AM #

louboutin

Some of the Osseous matter,Thank you so much.

louboutin People's Republic of China |

8/17/2011 11:21:46 AM #

Schlank schlafen

Wenn Du dich schlank schlafen willst, dann solltest Du unbedingt diese Seite besuchen: http://www.abnehmenimschlafinfo.com

Schlank schlafen Germany |

8/28/2011 6:07:58 PM #

Chris

Scafolding has helped or developments to gain market traction. tank you

Chris United States |

9/6/2011 4:25:01 AM #

Gabriel Jefferson

Exceedingly insightful many thanks, I reckon your current subscribers would most likely want a great deal more posts such as this maintain the excellent hard work.

Gabriel Jefferson United States |

9/6/2011 3:00:01 PM #

Michael Cole

Exceedingly challenging many thanks, I think your trusty subscribers may possibly want more stories such as this continue the good effort.

Michael Cole United States |

9/6/2011 6:59:09 PM #

Tim Anderson

Tremendously entertaining cheers, I reckon your readers could perhaps want significantly more items of this nature keep up the good effort.

Tim Anderson United States |

9/7/2011 4:55:05 AM #

Andrew Smith

Awfully interesting bless you, I reckon your current readers would probably want considerably more reviews like that carry on the good hard work.

Andrew Smith United States |

9/7/2011 3:06:00 PM #

Range Rover Repair Atlanta

interesting but I'm still a little confused.

Range Rover Repair Atlanta United States |

9/7/2011 3:12:03 PM #

Gabriel Jefferson

Surprisingly revealing appreciate it, I reckon your current followers could very well want way more blog posts such as this maintain the great content.

Gabriel Jefferson United States |

9/8/2011 1:20:19 PM #

Tim Jefferson

Very entertaining bless you, It looks like your current followers could perhaps want a good deal more articles such as this maintain the excellent work.

Tim Jefferson United States |

9/9/2011 1:54:49 AM #

Gabriel Smith

Particularly insightful thank you, I'm sure your current visitors may possibly want further items similar to this keep up the great effort.

Gabriel Smith United States |

9/14/2011 7:45:51 AM #

blu cigarette review

But a shortcoming of MVC Scaffolding is that, out of the box, it works only in Code First mode.

blu cigarette review United States |

9/16/2011 10:21:10 AM #

reg cure

Really the blogging is spreading its wings rapidly. Your write up is a fine example of it.

reg cure United States |

9/16/2011 3:56:15 PM #

Home Office Furniture Store Atlanta

great stuff!
love your blog!

Home Office Furniture Store Atlanta United States |

9/18/2011 9:25:41 AM #

sky blue credit repair

You completed certain fine points there. I did a search on the subject matter and found most folks will consent with your blog.

sky blue credit repair United States |

9/19/2011 7:32:32 AM #

Dave

Awesome post! It\\\'s so hard to find good information on Internet Marketing on the internet nowadays with all the fluff that is out there. I look forward to reading more of your articles and keep up the good work. Smile

Dave |

9/20/2011 3:10:55 AM #

perforated metal

I am not sure where you are getting your information, but good topic. I needs to spend some time learning much more or understanding more. Thanks for fantastic info I was looking for this information for my mission.

perforated metal United States |

9/21/2011 1:35:53 AM #

michelle

Thank you for another essential article. Where else could anyone get that kind of information in such a complete way of writing? I have a presentation incoming week, and I am on the lookout for such information. <a href=\\\"http://fixtinnitusfast.com\\\">find more...</a>

michelle |

9/22/2011 5:28:29 AM #

Web Hosting

Thanks for taking the time to discuss this, It clear some points to me, well keep on posting on different subject matter If possible, as you gain expertise, It is extremely helpful for me.

Web Hosting India |

9/22/2011 6:32:46 AM #

sjr bangalore

I like your web site, I was emailed the link, will bookmark you and visit again enjoyed reading this post

sjr bangalore United Kingdom |

9/24/2011 1:06:33 AM #

james

Hi
I’m going to spend more time learning about this topic

james |

9/27/2011 8:09:09 AM #

dspangler731

Thanks for your post. My spouse and i have generally noticed that a majority of people are eager to lose weight when they wish to show up slim along with attractive. Even so, they do not constantly realize that there are many benefits for you to losing weight in addition. Doctors state that obese people experience a variety of illnesses that can be directly attributed to their particular excess weight. The good thing is that people that are overweight and also suffering from diverse diseases can help to eliminate the severity of the illnesses by way of losing weight. It is possible to see a progressive but marked improvement in health when even a negligible amount of weight reduction is achieved.

One thing I would like to discuss is that fat reduction plan fast can be performed by the appropriate diet and exercise. Your size not merely affects the look, but also the complete quality of life. Self-esteem, despression symptoms, health risks, in addition to physical ability are influenced in an increase in weight. It is possible to make everything right but still gain. If this happens, a problem may be the offender. While an excessive amount of food instead of enough exercising are usually at fault, common medical ailments and popular prescriptions can greatly enhance size. I am grateful for your post right here.

I acquired more a new challenge on this fat reduction issue. Just one issue is that good nutrition is especially vital whenever dieting. A huge reduction in junk food, sugary ingredients, fried foods, sweet foods, red meat, and whitened flour products may be necessary. Having wastes unwanted organisms, and contaminants may prevent desired goals for fat loss. While specific drugs quickly solve the matter, the unpleasant side effects are certainly not worth it, and they also never provide more than a non permanent solution. It can be a known undeniable fact that 95% of dietary fads fail. Many thanks sharing your thinking on this blog site.

Thanks for the tips shared on your blog. Yet another thing I would like to talk about is that weight-loss is not supposed to be about going on a dietary fads and trying to get rid of as much weight that you can in a few days. The most effective way to burn fat is by getting it little by little and using some basic points which can assist you to make the most from your attempt to lose weight. You may recognize and already be following many of these tips, nevertheless reinforcing awareness never does any damage.

I believe  avoiding refined foods could be the first step to be able to lose weight. They will taste great, but processed foods possess very little vitamins and minerals, making you consume more to have enough power to get through the day. If you are constantly ingesting these foods, changing to whole grains and other complex carbohydrates will aid you to have more strength while having less. Interesting blog post.

dspangler731 |

9/27/2011 1:56:42 PM #

dspangler731

Thanks for the article. I have constantly noticed that a majority of people are wanting to lose weight as they wish to show up slim and attractive. Even so, they do not constantly realize that there are more benefits for losing weight in addition. Doctors assert that overweight people are afflicted with a variety of disorders that can be directly attributed to their particular excess weight. The great news is that people who\'re overweight and also suffering from several diseases can reduce the severity of the illnesses by way of losing weight. You possibly can see a continuous but identifiable improvement in health as soon as even a negligible amount of fat loss is realized.

One thing I would like to comment on is that fat burning plan fast is possible by the proper diet and exercise. A person\'s size not merely affects appearance, but also the overall quality of life. Self-esteem, major depression, health risks, and physical abilities are influenced in an increase in weight. It is possible to just make everything right but still gain. In such a circumstance, a problem may be the reason. While too much food and never enough exercise are usually responsible, common medical ailments and widespread prescriptions can greatly enhance size. Thanks a bunch for your post right here.

I figured out more something totally new on this fat reduction issue. One particular issue is that good nutrition is highly vital while dieting. An enormous reduction in bad foods, sugary foods, fried foods, sugary foods, red meat, and white flour products could be necessary. Possessing wastes organisms, and toxins may prevent desired goals for fat loss. While certain drugs briefly solve the issue, the nasty side effects usually are not worth it, plus they never present more than a momentary solution. This can be a known incontrovertible fact that 95% of fad diets fail. Thanks for sharing your opinions on this weblog.

Thanks for the suggestions shared on the blog. Something else I would like to say is that fat reduction is not about going on a fad diet and trying to shed as much weight as you can in a couple of weeks. The most effective way to shed weight is by having it gradually and obeying some basic recommendations which can provide help to make the most out of your attempt to slim down. You may learn and already be following most of these tips, nevertheless reinforcing information never does any damage.

I believe that avoiding ready-made foods could be the first step in order to lose weight. They may taste fine, but refined foods include very little vitamins and minerals, making you eat more simply to have enough vigor to get over the day. In case you are constantly having these foods, changing to grain and other complex carbohydrates will help you have more energy while eating less. Good blog post.

dspangler731 |

9/27/2011 1:56:55 PM #

dspangler731

Thanks for your posting. My partner and i have always noticed that almost all people are desperate to lose weight since they wish to look slim as well as attractive. However, they do not constantly realize that there are many benefits so that you can losing weight additionally. Doctors assert that obese people are afflicted by a variety of illnesses that can be directly attributed to their own excess weight. Fortunately that people who\'re overweight and suffering from numerous diseases can help to eliminate the severity of their illnesses simply by losing weight. It\'s possible to see a slow but marked improvement with health as soon as even a bit of a amount of losing weight is obtained.

One thing I\'d like to reply to is that weight loss program fast may be accomplished by the perfect diet and exercise. Your size not simply affects the look, but also the quality of life. Self-esteem, despression symptoms, health risks, along with physical capabilities are disturbed in weight gain. It is possible to make everything right and still gain. Should this happen, a problem may be the perpetrator. While a lot food but not enough work out are usually guilty, common health conditions and popular prescriptions may greatly increase size. Thanks alot : ) for your post right here.

I mastered more interesting things on this weight loss issue. One issue is that good nutrition is highly vital whenever dieting. A big reduction in junk food, sugary ingredients, fried foods, sugary foods, pork, and white-colored flour products can be necessary. Possessing wastes parasitic organisms, and contaminants may prevent aims for losing fat. While certain drugs momentarily solve the matter, the terrible side effects are usually not worth it, they usually never offer more than a short-term solution. It is a known indisputable fact that 95% of fad diets fail. Many thanks for sharing your notions on this weblog.

Thanks for the suggestions shared on your blog. Something else I would like to convey is that weight loss is not information about going on a celebrity diet and trying to lose as much weight that you can in a couple of weeks. The most effective way to shed weight is by taking it little by little and using some basic suggestions which can help you to make the most out of your attempt to drop some weight. You may realize and be following some tips, nevertheless reinforcing awareness never does any damage.

I believe that avoiding prepared foods is a first step to help lose weight. They will often taste great, but packaged foods possess very little nutritional value, making you try to eat more simply to have enough strength to get over the day. When you are constantly taking in these foods, transitioning to whole grain products and other complex carbohydrates will make you to have more vitality while feeding on less. Good blog post.

dspangler731 |

9/27/2011 4:27:11 PM #

Photos printed on canvas


Pictures, prints and onto canvas created from your own photos, framed and ready to hang. For the best quality canvas prints at the lowest online prices guaranteed, with fast delivery.
http://www.picstocanvas.co.uk/

Photos printed on canvas United States |

9/29/2011 6:15:48 AM #

HCG Recipes

Love the article.  I will gladly post a do follow trackback if this comment is approved.  Also - feel free to use our hcg diet recipes on your site.

HCG Recipes |

9/29/2011 1:32:48 PM #

Andrew Cole

Really revealing thank you, I believe your current readers would certainly want further information along these lines keep up the great work.

Andrew Cole United States |

9/30/2011 3:56:34 AM #

French Casinos

Top notch posting. We constantly see your site and determined lots of gorgeous work. With thanks with your most endeavors you accomplish tricky.

French Casinos United States |

9/30/2011 1:21:56 PM #

Tim Jefferson

Very enlightening cheers, I believe your readers will likely want way more reviews like this carry on the excellent content.

Tim Jefferson United States |

10/2/2011 5:22:40 PM #

Once a fat girl

I had lost all motivation until I found your website. I love the information here and will be bookmarking it to come back again. I am also going to let my friends know about your blog

Once a fat girl |

10/2/2011 10:42:36 PM #

Johnny Jefferson

Astonishingly useful thanks, I do believe your readers will likely want a whole lot more writing such as this keep up the great effort.

Johnny Jefferson United States |

Comments are closed