Thursday, May 6, 2010

Act 1: ASP.NET MVC2 and Strongly Typed HTML Helpers and EditorExtensions - OH MY!

Scene 1 - Intro

This is part one of a multiple post series that will dive into some of the new features of ASP.NET MVC2 and Visual Studio 2010.

So, Visual Studio 2010 and MVC2 have been a couple for a month. I hope everyone has gotten their hands a little wet or better yet, completely drenched and wrinkly, but not to the point of hypothermia, of course.

I'll admit that I have grown an obsession to the Strongly Typed HTML Helpers, as described here by Scott Guthrie, and the scaffolding capabilities within MVC2. This stuff is pretty slick and follows the pattern that, I believe, Microsoft has laid out with ASP.NET and Visual Studio for rapid development.

What am I getting at?

Meat & Potatoes

Scene 2 - Mongo Meets EditorExtensions

Being the inquisitive developers we are we decided to take a look at the EditorExtensions in System.Web.Mvc.Html and what they can provide out-of-box. We've done a lot of work in MVC1 with strongly typed models, custom views, routing, and jQuery to build a pretty neat platform. Our thoughts really boiled down to: what are the instant take aways and how can we apply the new tools in our future work? Thus begun the retrieving of hard hats and shovels.

If you're not familiar, MVC2 provides the option of creating strongly typed views. When creating a new view the ability is there to now associate the view to a model and specify the CRUD behavior in the IDE.

This is where I broke ground.

Putting It Together

Scene 3 - Mongo's Turbulent Relationship

I began by creating a model, Department, and added a couple properties and specified some metadata for the properties.



I personally like to code a little and then move onto the next step; which in this case would be creating my view. I selected create strongly type view and decided that my CRUD behavior would be Edit - select from the View Content dropdown.



So, with a little help from my friend MVC2 and VS, I now have a view that provides me with text boxes using the TextBoxFor and the LabelFor extensions. Needing instant gratification I built the solution and ran it to see my Department model in its editable glory.



Knowing that I didn't foo'bar anything I went back and decided to add some more properties to the Department model. Being satisfied with the new properties I wanted to update my view to reflect these changes. I right-clicked on my view and much to my surprise there was no "refresh my view," or the like option. I just hit the buried utility line (remember, call before you dig) and stopped all work to assess the situation and whine. Beggers can't be choosers; I got over my disappointment I won't be able to do the bare minimum to get this to work, so I dug a little deeper.



Scene 4 - Mongo Work Smarter, Not Harder

Knowing I was looking for an edit option and the awesome intellisense in VS2010 I narrowed my options to the EditorFor... extensions and struck oil: EditorForModel(). I added this in under what was provided when creating the strongly typed view for comparison purposes.



Building and running the application I can now see all the properties as editable fields using EditorForModel. Pretty f'in cool, one method call and I get the labels and text boxes for my model.



This got the steam engine in my head fired up and wondering, "This is great but what happens if I have read-only properties in my model?" or, "What if I want to have multiple edit views for my model?" Now my hole is filling with water, and I really hate when that happens because I never have the appropriate footwear.

Crap, I now have to revert and actually do work by manually adding in all those labels and text boxes to the view to exclude the properties I do not wish to edit, or exclude these properties from my model. Being stubborn and for the sake of the argument I don't have the option to remove any properties from the model.

Making sure I'm not reading the blueprints upside down I referred to the EditorExtensions reference to see if I can query my model to display the fields for the properties I specify.

No dice.

Scene 5 - Mongo Has Salt Poured on His Wounds

All the EditorForModel descriptions begin, "Returns an HTML input element for each property in the model..." NOOOOOOOOO, not for each property!

After stomping my feet and whining some more, I went and got the heavy artillery: Sean Iannuzzi. He's our architect and figured he'd throw me a bone, tell me I missed something, pat me on the head, and send me on my way. False. After explaining the situation and him working thru the disappointment, it was time to take action...

Intermission. Fin Act 1.

Mongo Sez (Summary)

  1. Freshin' up a little on MVC2 Strongly Typed Html Helpers with Scott Guthrie.
  2. Can has out-of-box strongly typed views.
  3. EditorExtensions - can't live with 'em, can't live without 'em.
  4. I am roller coaster of emotions when coding, aka: code emo.
I will be posting Act 2: ASP.NET MVC2 and Constraints on Type Parameters and Crazy Code - OH MY! in the coming days.

If I've depicted anything incorrectly or am making any blatant oversights - which happens when I'm getting code emo - please feel free to leave comments. Also feel free to leave any comments/questions regarding Html Helpers or the Editor Extensions. I'll write back and update as I can.

No comments:

Post a Comment