About the book
This is the second chapter
of the book ASP.NET MVC in Action. It has been
published with the exclusive permission of Manning.
Jeffrey Palermo, Ben Scheirman, Jimmy Bogard, Eric Hexter, and Matthew HinzePages:
Get 30% discount
DotNetSlacker readers can get 30% off the full print book or ebook at www.manning.com using the promo code
dns30 at checkout.
A model is a representation of something meaningful. It is not necessarily something physical, but something real:
a business concept or an API that’s difficult to work with.
When we write object-oriented software, we create classes that make up this representation. We can create our
representation so that when we use it we are working in our natural human language, like English or Spanish, or
business jargon, instead of in mere programming language constructs like booleans, meaningless strings, and
When working with a UI framework like ASP.NET MVC, the UI is the complex problem that we manage. It’s the
data in a window, a form submission from a user, the options in a select list. While model is an overloaded term in
software, we’ll be talking about the presentation model, the model that represents the screen and user input from
the screen of an application. More specifically, we will focus on representing user input.
Just like a presentation model represents a display, we craft a model to represent the data coming into our
application. And just like a strong presentation model makes it easy to work with our data in the view, a strong
input model makes it easy to work with user input in our application. Instead of working with error-prone string
keys and inspecting request values that hopefully match input element names, we can leverage ASP.NET MVC 2
features to work with a strong input model.
Designing the model
This simple form in Figure 1 has two text boxes and a check box.
As a feature of our application, it’s also worthy of a formal, codified representation—a class. Designing the class
to represent this form is easy: it’s two strings and a boolean value.
The input model in listing 1 is a simple class with a focused job.
Listing 1: The input model
#1 A property represents a textbox
#2 Represents input in second textbox
#3 Represents the checkbox
It is the surface area of user input--nothing more, nothing less.
Presenting the input model in a view
Views can be configured with the input model as the ViewData.Model type. We craft the HTML form using the
input model. ASP.NET MVC 2 ships with several helpers that ease this and allow for strong associations between
form element names and model property names. The form in listing 2 is built with our input model, NewCustomerInput from listing 1.
Listing 2: A view using the input model
#1 Again, specifying the model
#2 A helper for the label
#3 A helper for textboxes
#4 A helper for checkboxes
Note the special HTML Helpers that take a lambda parameter (#2). These helpers render HTML form elements
with the name attribute set to the name of the property expressed in the lambda. When working with HTML forms,
work must be done to ensure that the software is looking for values in the known location.
Lambda expressions aid in refactoring
Don’t underestimate the value of lambda expressions in your views. These are compiled along with the rest of
your code, so if you rename an action, for example, this code will break at compile time. Contrast this with code
in your ASPX that references classes and methods with strings. You won’t find those errors until runtime. Having
strongly typed view data references also aids in refactoring. Using a tool like JetBrains ReSharper
(http://www.jetbrains.com/resharper) will allow you to refactor code and have it reach out to all of the views that
use it as well. Very powerful, indeed.
Before strongly-typed helpers, we relied on magic strings and there was effort to ensure consistency between
the input form and the processing logic. With strongly-typed helpers like we use in listing 2, ASP.NET MVC 2
handles this coordination for the developer, so renaming a property won’t cause our screen to malfunction.
Working with the submitted input
The form in listing 2 posts to the Save action, and ASP.NET MVC 2 offers a convenient way to translate the values
in the HTTP request to our model. This process is called model binding, and we’ll take a quick look at it now in
Listing 3: Model binding form values to the input model
By declaring the action’s parameter as a
NewCustomerInput object, the value is wired-up by ASP.NET MVC
DefaultModelBinder and delivered properly (#1). This is the default behavior in ASP.NET MVC 2.
Our action works with our strong input model object and not a dictionary of key value pairs (#2). In this case,
it’s not doing much; we’re just sending it as the model of a different view, so in the example we can inspect the
saved values. But, in a real action, we’d have the opportunity to work with it as with any other class—persist it or
pass it along to collaborating classes for further processing.
Many views are not just displays or input forms but combine elements of both to achieve a rich user experience.
By representing user input with an explicit model object, we can use ASP.NET MVC 2 model binding to work with
With strong presentation models comes an avalanche of simplicity that enables maintainability and rapid
construction. Refactoring, renaming, adding fields, and changing behaviors have returned to the world of
programming. Freed from the shackles of the designer and a constant effort to maintain consistency across a
myriad of magic strings that may or may not make sense, developers can focus on one thing at a time. The model
is at the core of Model-View-Controller.
Get 30% discount
DotNetSlacker readers can get 30% off the full print book or ebook at www.manning.com using the promo code dns30 at checkout.
Manning Publication publishes computer books for professionals--programmers, system administrators, designers, architects, managers and others. Our focus is on computing titles at professional levels. We care about the quality of our books. We work with our authors to coax out of them the best writi...
This author has published 33 articles on DotNetSlackers. View other articles or the complete profile here.
Please login to rate or to leave a comment.