Online Storefront with MVC 3 / Razor (Create the Project) – 2

by Aref Thu, November 25 2010 18:42

Welcome back, this is part 2 of the MVC 3 / Razor storefront series. In this post, we will set up the project, create the database structure and set up some folders.

To begin, I will assume that you have installed the MVC 3 Release Candidate bits. If not, you can get it at My development environment is Visual Studio.NET Ultimate 2010. You will need .NET 4 (Framework), ASP.NET 4 and Visual Studio.NET 2010 or Visual Web Developer 2010 to develop this particular solution. As part of the MVC 3 installation, you will also get the wonderful open source, package manager, NuGet. This greatly simplifies the installation of extensions (and we may need some). Also, I am using SQL Server 2008 Express Edition for this solution.

Okay, let’s get going…

1. Creating the project

Create a new project and choose the “ASP.NET MVC 3 Web Application” template as shown below.


2. Choose the project template

Clicking on OK will launch the choice dialog box for selecting the project template as shown below.


Go ahead, and choose the “Internet Application” template. You may additionally check the “Create a unit test project” check box but I will skip TDD for this project.

2. Creating the MembershipProvider support

Now run the application by hitting the F5 key and it should launch under the local ASP.NET Development server over a non-standard port. Viola! Here is your application with the ability to login, and register.


Now, register a new user by first clicking on the “Log On” link at the top right corner of your application and then on the “Register” link in the subsequent page. I created a user called Admin for now.


Clicking on the Register button will in turn create a database called ASPNETDB under the App_Data folder. Let us add this to our solution explorer by right clicking the App_Data folder and adding an existing item and choosing the ASPNETDB.MDF file to be added. You should now be able to double click on the ASPNETDB database under the solution explorer and have it appear under the “Data Connections” window.

 This will also create a connection string called “ApplicationServices” in the web.config to the default ASPNETDB database to support the MembershipProvider. In fact, the Membership, Profile and roleManager providers all point to this connection. For this project, we will add our own store schema to the same database. (I may cover how to use your own custom Membership provider with your own database or you may look it up on the internet).

3. Creating the database schema

We will now add the required tables to the database. Specifically we will add the following:








The entire database is attached and you may download it.

4. Creating the Entity Framework Model

Now we will create an entity framework model from our database schema. To do this, we will

1. Right click on the Models folder within the Solution Explorer and click on add… new item.


2. Click on Add and then choose “Generate from Database”


3. In the Entitiy Data Model Wizard, (which like a wizard recognizes your database from the connection settings defined in the web.config), change the entity connection settings name to MystoreEntities and click on Next.


4. In the “Choose Your Database Objects” step, select the following tables and change the Model Namespace to “MystoreModel”.

Cart, Category, Customer, Order, OrderItems, Product as shown below.


5. Now click on Finish to close the wizard and have the EF object model created. You will now see the object model generated as the edmx file as shown below.


Conclusion and next steps…

So, we have just created a blank MVC 3 / Razor solution, added a database with membership and role support and support for the online store. We then exposed this as an object data model using the Entity Framework.

Next time, we will create some templates, add validation at the model level and showcase some (featured) products on the home page using the model-view-controller.

Until next time, take care… (234.58 kb)


Tags: , ,


Online Storefront with MVC 3 / Razor (Introduction) – 1

by Aref Wed, November 24 2010 19:47

So, what are we going to do?


This is quite exciting. I am starting fairly new to MVC/Razor and hopefully my journey will resonate with some of yours.

This is the first in a series of posts that I have planned. In this post I will describe the various concepts and areas that I wish to explore as the online storefront application gets developed.

In no particular order, here are some of the concepts that we shall explore.

Model driven data validation (DRY – ‘Don’t Repeat Yourself – concept)

Data Annotation validation is quite powerful. By using the Data Annotation Model Binder, you can provide data validation

at the model level that is reflected in all views that are constructed using that model or derived from that model.

Morover, using the MetaDataType attribute, you can include validation support at the Entity Framework level.

Using the ViewModel

Using ViewModel, we will be extending the underlying model to provide richer support of the model to the view.

Using filter driven support for security

We will be using filters to provide membership support. We will also be looking at the default membership provider to provide support for authentication.

Using templates for views

We will use templates to provide a consistent look and feel to the site. We will explore the flexibility offered by Razor

in rendering sections of the template and the default section for the main content.

Using strongly typed Html Helpers

Using these helpers, we will achieve better type safety at build time and get the benefit of compile time checking of the view for errors.

Using Entitiy Framework as the base Model

Using EF, we will be able to quickly bring in our base database model into our application at the Model layer.

Using Webgrid with sorting and pagination support

We will use the WebMatrix webgrid to provide for a grid representation of the model with support for pagination and sorting of columns.

Providing for Localization

We will look to provide localization for labels and system messages including validation messages. The intent is not to translate or localize user submitted data.

Tags: ,


Hello World

by Aref Tue, November 23 2010 19:51

Every once in a while, I find myself going off in spurts of enthusiastic pursuit of new (and hopefully improved) ways in engaging with technology. Often times, it is related to a specific challenge faced in the course of day to day life, but sometimes, they are more academic in nature, such as the curiosity in the amount of code reduction in using the MVC pattern over the traditional ASP.NET approach in building web applications.

I will usually complete these exercises, sometimes to my satisfaction, but hardly ever have I tried to document my experience for the sole purpose of sharing.

Today, I thought that I’d start my blog and begin documenting some of these experiences. Frankly, it was quite intimidating to begin a public log open to scrutiny. The technical aspect of throwing up a blog site was a piece of cake, thanks to the wonderful and easy to deploy blog engine, BlogEngine.NET (look for it in codeplex). I downloaded the bits, modified the robots.txt, personalized the site, web-deployed it over at my ISP and I was off and running.

Most recently, I have started to build an online shopping application using MVC 3 RC and will be blogging about it. Maybe you’ll find value in some of the bits that I share and I’m sure that I will learn from the feedback that you will provide.

Until next time, be well.