SingingEels : Development Community & Resource

Login

Articles

  • ADO.NET (2)
  • ASP.NET (36)
  • LINQ (4)
  • Security (2)
  • Silverlight (3)
  • SQL (7)
  • Standards (5)
  • WCF (2)

Syndication

  • Articles RSS
  • Blogs RSS

Contribute

  • Our Authors List
  • Member Sign-Up
  • Suggestions Box
ASP.NET Hosting with MS SQL 2008 – Click Here!

ASP.NET MVC in the Real World

(Jun 18 2008 - 09:32:09 PM by Timothy Khouri) - [print article]
ASP.NET Hosting with MS SQL 2008 – Click Here!

MVC (the "model view control" pattern) isn't new, but it is new to ASP.NET. If you're like me, you may have been impressed by a demo, but you've probably thought "how does this work in the real world?" I hope to answer that question in this article.

All of the MVC demos that I have seen show how quick and easy it is to get going with ASP.NET MVC (from here on I'll just say "MVC"). They show a CSS'd up site with a navigation section, and a main content section. And in very little code, and very little time, they show how to retrieve data from your Model (the "Controller" does this), and how to display it in your page (the "View" does this). It look so simple, but there are some design questions that need to be addressed. We'll discuss them as we look into MVC.

MVC and Web Sites

The gist behind MVC is that a "View" is only responsible for displaying it's data, and nothing else. This means that if you had a View named 'UsedCars', it is only responsible for listing out used cars. There is no code for adding, deleting or updating the data in a View itself; there is no provision for displaying anything else other than "used car data".

So, MVC in a web application means that a View is not responsible for the navigation, header, footer or any other part of the site. With that being said, a few questions come to my mind. These are the items that this article will discuss as our 'real world' issues:

  1. How does MVC handle dynamic sections on a website that are not part of the current "View"? - For instance, on SingingEels.com, if you're logged in, the navigation section changes to include member's only links. When you're looking at an article, the "related section" on the right of the site will show you blog posts that are similar to the content you are looking at.
  2. How does MVC work with validating a user input, and displaying a friendly message back to the user on a screen that he expects to see? The reason I mention this is because in MVC, an HTML form on one View will be posting to an Action (a different URL) that actually does the work. This is different than in the "Web Forms" world, where the page posts-back to itself. So if the user entered some invalid data, how do you send them back to the original View, fill out the form back to how they had it, and display the error message?

Aside from these "issues" above, we'll see how to achieve common functionality (that we all do every day in the "Web Forms" world). These include things like creating paging functionality (I won't explain this in the article, but it's in the source code at the end). So, to demonstrate how to build a "full featured" MVC site that needs to solve the above issues, we're going to build a 'used cars' website. There will be a list of cars for sale (that will be paginated), displaying results from a database. We'll also have a few static pages and a "contact us" form. I'll include the entire source code and project at the end of the article. Keep in mind, this article and demo was written for ASP.NET MVC Preview 3.

Building Our MVC Layout with MVC Master Pages

The MVC design pattern was not originally for web applications, so extending this architectural pattern to the web allows for some breathing room. This is where we will answer our first question: "how do we handle dynamic sections in our web site that isn't part of a particular View?" Well, the answer is the same for how we handle our entire web site (the layout and styling of it) outside of any one particular View - with Master Pages!

So the first thing we are going to do with our MVC "Used Cars" website, is to create our basic layout and styles, and make a 'stub' for our Views to live in (basically, a ContentPlaceHolder). Here's what our site will look like:

SingingEels_MVC Used Cars Site

Now, there are a few new tricks that we get with ASP.NET MVC (yes, I used the whole name here - for a purpose). The .NET team has provided a lot of powerful "helper functions" that snap right into the MVC framework. So, to make the navigation links above, I would normally have done this:

<a href="/Home">Home</a>

<a href="/ForSale">For Sale</a>

<a href="/About">About Us</a>

... But the problem here is that we are making links to "pages" instead of making links to "MVC Views". This forces our file structure to be very tied to our web design, which may not be what you're going for. Instead we'll do this:

<%= Html.ActionLink("Home", "Index")%>

<%= Html.ActionLink("For Sale", "ForSale")%>

<%= Html.ActionLink("About Us", "About")%>

This allows us to define our links in terms of pointing to a 'View', as apposed to being tied to a specific URL. You don't have to build your links this way, but it helps if you decide to change your route logic and you want your site to automatically update the links. Since we have our basic layout done, let's take a look at how these pages work.

Simple ASP.NET MVC Views

For simple Views that don't have a lot of logic around them, you don't have to do much in your controller. So for the home page and the about us page, this is basically all of the code in my controller:

public ActionResult About()
{
   this.ViewData["Title"] = "About Page";

   this.ViewData["PageHeading"] = "Learn All About Us";

   return this.View();
}

public ActionResult Index()
{
   this.ViewData["Title"] = "Home Page";

   this.ViewData["PageHeading"] = "Welcome to Used Cars by Eels!";

   return this.View();
}

Then, in our View, we can display the data. Normally this would go in the 'aspx' or 'ascx' files, but I'm displaying the above data in the master page (since the title of the page, and the page heading could be considered part of the standard layout). Again, you can download the source code for this project at the end of the article to see for yourself.

Real-World MVC Issue : Data Validation

So, let's get out of the boring, and talk about the other big problem that needs to be solved - form validation. When a user fills out our contact us form, we want to check for required fields and alert the user when the form is invalid. If all goes well, we'll send the user to a "Thank You" page.

Here's what our page will ultimately look like:

SingingEels_MVC - contact us form being validated

But the question remains: how do we do it? First, we build our HTML form using the helper methods:

<form action="<%= this.Url.Action("SendContact", "Home") %>" method="post">
   First Name <%=this.Html.TextBox("FirstName", Request["FirstName"]) %>

   Last Name <%=this.Html.TextBox("LastName", Request["LastName"]) %>

   <!-- etc -->
</form>
Update: Jun 21 2008

You can also build forms by using the following helper method:

<% using (this.Html.Form("SendContact", "Home")) { %>

   <!-- form stuff here -->

<% } %>

Then, in our Controller we will do the validation:

List<string> errors = new List<string>();

if (string.IsNullOrEmpty(firstName))
   errors.Add("First Name is a required field.");

if (string.IsNullOrEmpty(lastName))
   errors.Add("Last Name is a required field.");

// etc... and now the important part!


if (errors.Count > 0)
{
   // In order to pass data on to a page that we are

   // redirecting to, we use the 'TempData' field.

   this.TempData["ErrorMessages"] = errors.ToArray();

   // Because the form is invalid, redirect back to the

   // View and pass back the form data.

   return this.RedirectToAction("Contact", new
   {
       FirstName = firstName,
       LastName = lastName,
       EmailAddress = emailAddress,
       Message = message
   });
}

The last piece of the puzzle is back in the contact us View. It's where we check the TempData field to see if there are any error messages in there, and if so, we display them. There are a few things to be concerned about here with using the TempData though. Mainly, it's important to know that behind the scenes, MVC is using the Session object to store the data between hits. Now, it is very quick to clean it up (immediately after the next request)... but if you're using a web-farm, it's important to remember that you'll need a shared session pool.

In Conclusion

MVC is a beautiful new tool that will no doubt be invaluable in the future. It does require a different approach to architecting your web sites, but it can be very worth it in the end. However, it's also important to remember that MVC is not replacing Web Forms.

I hope this article has shown that you can still achieve the functionality that you're used to having using ASP.NET MVC. Check it out for yourself and see if you don't grow to love it (or at least appreciate it) as I have. And of course, here's the source code for the above project: SingingEels_MVC Used Cars Demo.zip. Again, it was written and compiled for ASP.NET MVC Preview 3.

  • Jun 19 2008 - 07:53:43 PM Timothy Khouri

    I've been told by a friend of mine at Microsoft that the in "Html.TextBox" method, you don't need to tell it to pull from the Request object. It is *supposed* to check there automatically and try to grab the value based on the name (the first parameter). But that didn't work for me (maybe a bug in preview 3)?

  • Jul 23 2008 - 07:53:59 PM dc2533

    Tim,

    I just signed up. Great stuff !

    Any plans to take this to Preview 4 ?

    Thanks

  • Jul 24 2008 - 11:57:35 AM Timothy Khouri

    That's a great question, and the answer is basically... no :) I've already finished another article from Preview 4, and basically I want to keep moving forward with MVC.

    Also, this article is mostly informational, and the information *should* remain true going forward... We'll see!

  • Jul 24 2008 - 08:33:26 PM dc2533

    Thanks. When will the Preview 4 article be available for public

    consumption ?

  • Jul 24 2008 - 08:42:41 PM Timothy Khouri

    It's already there: http://www.singingeels.com/Articles/AJAX_Panels_with_ASPNET_MVC.aspx

  • Nov 05 2008 - 12:14:43 AM rajkumarmitt

    you means with ASP.net MVC, we are back to the old asp. where we would do all the validation manually. controls were the power of the asp.net , means all will be lost.

  • Nov 05 2008 - 12:15:35 AM rajkumarmitt

    you means with ASP.net MVC, we are back to the old asp. where we would do all the validation manually. controls were the power of the asp.net , means all will be lost. then why to use the asp.net MVC, even a code file also work as controller.

    http://rajmittal.blogspot.com

You must be logged in to add comments. If you have not already done so, you can create an account here. If you already are a member, you first need to login before you can comment.

Developer / Architect / Author

People to Follow

Experts in the categories related to this article.

  • Jonathan Carter

Related Blogs

These are the most recent blog posts related to this article.

  • A Change to the MVC "ActionSelectionAttribute"?
  • How to Handle "Side Content" in ASP.NET MVC
  • LINQ to SQL - Am I Hitting The Database?
  • ASP.NET MVC - Issue with CSS Class Name on ActionLinks
  • Site Updates, Bug Fixes and Syndication in .NET 3.5

Related Ads

SingingEels.com as of Jan 05 2009 - 07:56:44 PM - (0.0468738)