Tips

Contains tips and tricks for building ASP.NET MVC applications

Recently, I noticed a nice pattern in the Nerd Dinner application. Nerd Dinner uses strongly typed view model classes to pass data from a controller to a view. This pattern provides you with a convenient way of representing complex view data. Note: If you haven’t downloaded the Nerd Dinner sample ASP.NET MVC application yet, you really should. It is a really easy to understand sample application that you can use as a great introduction to ASP.NET MVC. You can download it from the http://www.ASP.net/mvc page. In an ASP.NET MVC application, you pass data from a controller to a view by using view...

Posted Monday, April 13, 2009 8:43 AM

I ran into a brick wall earlier today when I was attempting to create a controller with an Edit() action. I wanted to edit database records with LINQ to SQL. Here’s the controller action: Listing 1 – Edit() action [AcceptVerbs(HttpVerbs.Post)] public ActionResult Edit(Product productToEdit) { try { _context.Products.Attach(productToEdit, true); _context.SubmitChanges(); return RedirectToAction("Index"); } catch { return View(); } } This action...

Posted Wednesday, February 25, 2009 11:23 AM

By default, the ASP.NET MVC framework prevents you from submitting form data that contains potentially malicious content. This feature is called request validation. For example, submitting the following text in an HTML input field causes the ASP.NET MVC framework to throw an exception (Figure 1): <script>Alert(‘I am evil!’);</script> Figure 1 – An evil form post This is a good feature. You don't want people sneaking scripts into your website that can steal passwords or other sensitive user information. Normally, you want to leave request validation enabled. There are situations, however, when it is perfectly legitimate to want people to submit text that contains...

Posted Friday, February 20, 2009 3:40 PM

Yesterday, I was creating a simple HTML helper for displaying images. Nothing fancy. The code for the helper is contained in Listing 1. Listing 1 – Helpers\ImageHelper.cs using System.Web.Mvc; using System.Web.Routing; namespace MvcApplication1.Helpers { public static class ImageHelper { public static string Image(this HtmlHelper helper, string id, string url, string alternateText) { return Image(helper, id, url, alternateText, null); } public...

Posted Wednesday, February 18, 2009 4:34 AM

I created a sample ASP.NET MVC application that I plan to post at the http://ww.ASP.net/mvc website. While the application was being code reviewed by the ASP.NET MVC Feature team, a surprising objection surfaced. The application is extremely simple. It contains a view that renders a list of database records. Next to each record, there is an Edit link and a Delete link (see Figure 1). Pretty standard stuff. Or, so I thought… Figure 1 – A Grid of database records Here’s the objection. You should not use a link for deleting a record. Using a Delete link opens up a security hole....

Posted Wednesday, January 21, 2009 6:46 AM

In this blog entry, I list the essential tips and tricks that every developer who uses Visual Studio 2008 should know. I wanted to keep this list brief. I also wanted to focus on only those tips and tricks that I use on a daily basis. Almost all of these tips and tricks are just as useful regardless of whether you are building an ASP.NET Web Forms or ASP.NET MVC application. Tip #1 – You don’t need to select a line to copy or delete it I always cringe whenever I see someone select an entire line of code in the Visual...

Posted Tuesday, October 21, 2008 4:04 PM

In this tip, I explore one approach to building Ajax applications with ASP.NET MVC. I show how you can use view data when building Ajax applications with ASP.NET MVC in the same way as you would use view data when building server-side application. I demonstrate how to create a custom HTML Helper that renders client view data. One of the primary benefits of building an ASP.NET MVC application is that it enables you to build web applications that support a sharp separation of concerns. This sharp separation of concerns enables you to build applications that are highly testable and highly adaptable...

Posted Wednesday, October 08, 2008 4:44 PM

In this tip, I demonstrate how you can create a custom HTML Helper that you can use to generate a user interface for paging through a set of database records. I build on the work of Troy Goode and Martijn Boland. I also demonstrate how you can build unit tests for HTML Helpers by faking the HtmlHelper class. This week, I discovered that I desperately needed a way of paging through the database records in two of the sample MVC applications that I am building. I needed a clean, flexible, and testable way to generate the user interface for paging through...

Posted Thursday, September 18, 2008 7:25 AM

In this tip, I demonstrate how to take advantage of the validators from the System.ComponentModel.DataAnnotations namespace in an MVC application. You can take advantage of these validators to validate form data before submitting the form data into a database. In my previous tip, I explained how you can take advantage of the validators included with the Microsoft Enterprise Library Validation Application Block to validate an entity before submitting the entity to a database. The Validation Application Block supplies you with a very rich set of validators that support advanced validation scenarios. You can read about using the Validation Application Block in...

Posted Wednesday, September 10, 2008 11:20 PM

In this tip, I demonstrate how you can use the Microsoft Enterprise Validation Application Block within an MVC application to perform both basic and advanced form validation. The Validation Application Block supports a rich set of validators that you can begin using in an ASP.NET MVC application. In this tip, I show how you can use the Microsoft Validation Application Block to perform form validation within an ASP.NET MVC application. Using the Validation Application Block was surprisingly easy. You can download and start using the Validation Application Block in minutes. The Validation Application Block uses a model-driven approach to validation. You specify...

Posted Wednesday, September 10, 2008 2:50 AM