Scott Hanselman

ASP.NET MVC Cheesy Northwind Sample Code

March 12, '08 Comments [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. I am a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by ORCS Web
Wednesday, March 12, 2008 7:05:18 AM UTC
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!
Wednesday, March 12, 2008 7:32:16 AM UTC
Great.. thank you Scott
Thursday, March 13, 2008 12:50:39 AM UTC
Nice, that mini MVC flowchart looks tasty. You don't happen to have a link to that picture alone?
Thursday, March 13, 2008 1:31:59 AM UTC
Johan - That's just from the slides from my talk. You can download those.
Thursday, March 13, 2008 4:12:17 AM UTC
Thanks, for the samples..

Thursday, March 13, 2008 5:49:12 PM UTC
Excellent. I don't wonder why Microsoft hire you. ^^
Thursday, March 13, 2008 7:30:06 PM UTC
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
Tortilla Curtain
Thursday, March 13, 2008 8:12:19 PM UTC
Are you using it in IIS6? You might need to add .MVC to your routes as IIS6 doens't (easily) support extensionless URLs.
Thursday, March 13, 2008 8:34:09 PM UTC
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
Tortilla Curtain
Friday, March 14, 2008 1:00:28 AM UTC
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?
Tortilla Curtain
Friday, March 14, 2008 3:27:19 PM UTC
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.
Tortilla Curtain
Saturday, March 15, 2008 12:55:18 AM UTC
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
Piers
Saturday, March 15, 2008 3:09:14 AM UTC
Thanks for the sample!! Where are the unit tests?
Sunday, March 16, 2008 4:57:51 PM UTC
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.
Sunday, March 16, 2008 4:58:19 PM UTC
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.
Sunday, March 16, 2008 9:10:05 PM UTC
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.

Wednesday, March 19, 2008 1:37:02 AM UTC
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
Wednesday, March 19, 2008 10:35:11 AM UTC
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
noel
Sunday, April 13, 2008 10:15:53 AM UTC
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.