she swears <i>geek</i> is a term of endearment

Asp.Net MVC - yeaaaaaaaaaaaaaaaaaaaaaah

Aw, shucks guys, you rock!  The Asp.Net team: Scott "Gu" Guthrie, Scott Hanselman, Phil Haack, Rob Connery

Preview 5 was released last Thursday.  I’ve been buried in iPhone development so hadn’t seen that till this morning.  Reading the release notes was like receiving a "thinking of you" card out of the blue when you’d been away from home for a very long time.  If I sound dramatic, go build yourself and Objective C project and then fire up Visual Studio and write some Asp.Net MVC.  Its like  jogging with 20 lbs ankle weights for 3 weeks and then coming back to naked ankles.  …but I digress.

Preview 5 brings many of the kick-ass features I missed form Monorail and was hoping the Asp.Net MVC team might build for us. 

Model Class Binding

My favorite feature is "model binders".  This allows you to declare your action parameter as a strongly typed class instance and then marshall the request values into an object before you even hit the action.  Monorail had this built in as a convention where you could pass [param name].[property] from a request key and it would be initialized.  For example: given a class called "Car" with a property "TopSpeed", you could declare your action signature to be: public void Add( Car car) and then, in your html, pass <input type="text" name="car.TopSpeed" /> and magic would instantiate your Car class, set the TopSpeed property and pass it in your car parameter. 

With Asp.Net MVC, you can write ModelBinders to map the request keys to your model class and perform any necessary initialization or validation.  In addition, there a new ModelState collection that you can use for validation issues and awesome integration with the view side of things.  At first glance. it looks like there’s a whole lot of extra work to be done to accomplish this.  However, the team is planning to include standard modelbinder that uses the modelname.propertyname syntax for mapping incoming parameters.  All this ModelBinder goodness is for extensibility in those cases where you want to be super special or where your class is not that uses the modelname.propertyname.

The validation is pretty slick.  Personally, I use jquery.validate on the client side.   The rules that Scott demonstrates reminds me of this jquery library and I have a feeling it wouldn’t be too difficult to write a validation helper that could emit the server-side rules as a jquery registration.  While the jquery plugin does an awesome job on the browser side, it can be bypassed easily and having a server-side backup would be excellent.

Leave a Reply