Scott Hanselman

ASP.NET MVC Cheesy Northwind Sample Code

March 12, 2008 Comment on this post [19] Posted in ASP.NET | ASP.NET MVC | Programming | Speaking
Sponsored By

image A number of folks wanted the code from my talk at Mix, specifically the "complete application" example with CRUD (Create, Read, Update, Delete) so here it is.

It's nothing pretty to look at, but it makes a number of points with ASP.NET MVC:

  • It uses the Default Routes to do everything.
  • It uses a ViewUserControl to avoid duplication in a Form.
  • It includes the PagedList<T> that makes Linq to SQL easier in grids.
  • It's insanely simple. There's like nothing there and it works well.
  • It uses lambdas to generate URLs like:
    • Html.ActionLink<ProductController>(c => c.Edit(p.ProductID),"Edit")
    • Which says "if someone were to call Edit with ProductID as a parameter, what would the URL look like?" The system will then use the lambda along with the already declared routes and figure out the best URL. This is very DRY and lets us change our URLs in one place and have those changes reflected everywhere.

If you want to run it you need:

I known it's cheesy to do demos with Northwind, and we're actively working on more real world, more interesting samples. That said, I'd much rather show off your code, so if you do something cool and Open Source with MVC, let me know. BTW, I've added an ASP.NET MVC category to this blog.

UPDATE #1: PhilHa updated Brad Abrams older (and much more comprehensive) Northwind code to ASP.NET MVC Preview 2, so you can download that much better code below as well.

UPDATE #2: Looks like ASP.NET MVC Preview 2 runs pretty well on Mono. Anyone want to get Northwind up over there as well?

Technorati Tags: ,,

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Hosting By
Hosted in an Azure App Service
March 12, 2008 11:05
I happen to like the Northwind database. Its simplicity means that we are not distracted from what you are really trying to demonstrate. Everyone understands what categories and products are, so there is no time wasted learning the domain. I have seen demonstrations with 'real-world' databases and not learned a thing.

Thanks for publishing this example!
March 12, 2008 11:32
Great.. thank you Scott
March 13, 2008 4:50
Nice, that mini MVC flowchart looks tasty. You don't happen to have a link to that picture alone?
March 13, 2008 5:31
Johan - That's just from the slides from my talk. You can download those.
March 13, 2008 8:12
Thanks, for the samples..

March 13, 2008 21:49
Excellent. I don't wonder why Microsoft hire you. ^^
March 13, 2008 23:30
Scott:

I'm wondering if I missed some bit of information. I downloaded the MVC preview 2, but it behaves strangely on my XP machine w/ VS2008 vs my Vista PC with VS2008.

On the XP machine, if I create a project, I get prompted for the project name, but not for any solution info, etc. I then get prompted to create a test project and I am greeted with what appears to be a normal VS project screen. When I run the project for the first time (before making any changes), I get the dialog to modify the Web.config for debugging, which seems very normal. The app runs and I get the page I expect to see. However, after I save the project and try to run the app again (still with no changes), I am prompted to add a NEW web.config file because debugging is not enabled. This is weird! If I say OK, I am propmted to replace the file. If I say yes, I get a message that says "The operation could not be completed. Not implemented" If I choose to run without debugging, I get a page that says "Directory Listing -- /" and that's it.

I have a Vista virtual machine running under MS Virtual PC 2007 (on the same XP machine), and on that VM I have VS2008 with MVC Preview 2. I don't get any of the aforementioned errors on the VM. Is there an issue with XP? When I first select the MVC Web app template on the Vista VM, I am prompted for the project name, solution, etc., something that doesn't happen on the XP machine.

Any thoughts would be most appreciated.

Tortilla
March 14, 2008 0:12
Are you using it in IIS6? You might need to add .MVC to your routes as IIS6 doens't (easily) support extensionless URLs.
March 14, 2008 0:34
Sorry, I left out the web server info. No, I'm not using IIS, I'm using the ASP.NET Development Server. I'm literally running this as staight up as you can. It may well be something with my PC, but I figured I'd fish around to see if you or anyone else had heard of this type of behavior. I'm testing it out on my Vista VM and enjoying it very much. I'll try it on my home XP computer tonight and see what happens...

Tortilla
March 14, 2008 5:00
Scott:

I tried this on my home PC, and received the exact same behavior. The only differences in setup were that my home PC has IE 7.0 and VS 2008 Pro, whereas my work PC (the one I wrote about previously) had VS 2008 Team and IE 6.0. Any other ideas?
March 14, 2008 19:27
One more bit of info for you. I created a brand new XP SP2 VM and put VS 2008 Team on it. Then I put ASP.NET MVC Preview 2 on first, with no other ASP.NET MVC or 3.5 previews of any type. In this case, it worked just fine. I think something is going on with an XP machine that has a prevoious version of the ASP.NET 3.5 extensions preview, on which you instaall ASP.NET MVC Preview 2. The difference is visible right away, because on the VM I described above, when you create an ASP.NET MVC Web Application, you get three lines in the new project dialog (Name, Location and Solution Name). On the two other XP machines, I only got one line, Name, and the project wasn't saved right away. This wasn't an issue on the Vista VM, even though I had the previous 3.5 extensions loaded.
March 15, 2008 4:55
Hi Scott

I'm really quite taken with the MVC approach. One question, since you're looking at data access here, are there any plans to support an aysnchronous model within MVC.

Piers
March 15, 2008 7:09
Thanks for the sample!! Where are the unit tests?
March 16, 2008 20:57
Scott,

I really enjoyed the 4 screencasts on MVC, good stuff.

I have your sample from the demo and can't seem to run it and wondered if there was some configuration I might have missed. When I attempt to run with CTRL-F5, using the ASP.NET development server, I get just a directory listing when IE loads up and the app does not run at all.

I am on XP and can create my own MVC Preview 2 projects and they run just fine. I assume if I can run my own MVC 2 apps then yours should run as well. I opened your sample with both File-->Open->Project/Solution and tried File->Open->Web Site, both ran the same way.

Thoughts?

Thanks.
March 16, 2008 20:58
Scott,

I really enjoyed the 4 screencasts on MVC, good stuff.

I have your sample from the demo and can't seem to run it and wondered if there was some configuration I might have missed. When I attempt to run with CTRL-F5, using the ASP.NET development server, I get just a directory listing when IE loads up and the app does not run at all.

I am on XP and can create my own MVC Preview 2 projects and they run just fine. I assume if I can run my own MVC 2 apps then yours should run as well. I opened your sample with both File-->Open->Project/Solution and tried File->Open->Web Site, both ran the same way.

Thoughts?

Thanks.
March 17, 2008 1:10
Hi, Scott.

As I told you a few days ago, I have adapted the "Less Cheesy Northwind sample" for Visual Web Developer Express.

It can be downloaded from my blog. Although it is in Spanish, you'll see the link to the project's zip file at the bottom of the post.

Thank you very much.
Jose.

March 19, 2008 5:37
Why does the web.config have an http handler to restrict all .aspx files? This is not present in the default MVC template that you would create from New Project. Is there any reason you put this in?

<add verb="*" path="*.aspx" type="System.Web.HttpNotFoundHandler" validate="true"/>

Thanks,
-Jeff
March 19, 2008 14:35
Hi,
Scott,
I had a look at your examples and videos on asp.net site.
I have a question on deployment. In the real world we are going to deploy an application to a web server, app server and database server. Can you explain how this will work for a MVC application?
I presume the controller will need to be on the web server as routing will need to call to the appropriate controller based on the url. Is it here that we seperate what gets deployed to a server? i.e view and controllers are on the web server and model is on the app server
Thanks Noel
April 13, 2008 14:15
Hi Scott,
I love the sample videos & code, great work!

However i've been getting an error with the paged list results for page '0':

This:
http://localhost:52265/Product?page=0

Generates the error:
"The null value cannot be assigned to a member with type System.Int16 which is a non-nullable value type."
The error is pointing to this line in the PagedList method:

this.AddRange(source.Skip(index * pageSize).Take(pageSize).ToList());

Do you have any pointers as to why its not accepting a 0 value?

Many thanks

Ben
Ben

Comments are closed.

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.