29.05.2015 Views

o_19mgorv9t13a3ko71fev19l81mqa.pdf

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

You can also register custom model binders by decorating the model class with the ModelBinder attribute, which means<br />

that you don’t need to use the Global.asax file. In Listing 24-40, you can see how I have specified<br />

AddressSummaryBinder as the binder for the AddressSummary class.<br />

Listing 24-40. Using the ModelBinder Attribute in the AddressSummary.cs File<br />

using System.Web.Mvc;<br />

using MvcModels.Infrastructure;<br />

namespace MvcModels.Models {<br />

}<br />

[ModelBinder(typeof(AddressSummaryBinder))]<br />

public class AddressSummary {<br />

public string City { get; set; }<br />

public string Country { get; set; }<br />

}<br />

Summary<br />

In this chapter, I introduced you to the workings of the model binding process, showing you how the default model binder<br />

operates and the different ways in which the process can be customized. Many MVC Framework applications will only need the<br />

default model binder, which works nicely to process the HTML that the helper methods generate. But for more advanced<br />

applications, it can be useful to use custom binders that create model objects in a more efficient or specific way. In the next<br />

chapter, I show you how to validate model objects and how to present the user with meaningful errors when invalid data is<br />

received.<br />

661

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!