The MVC framework: getting started with the MVC controls

Ok, you’ve probably read Eilon’s post on the new MVC controls. I was asking for something like that when I mentioned that I dind’t think that codebehind files were evil. So, I thought it would be great to take a look at what’s available for the current release (even though the controls are still on the futures assembly).Today, we’re going to start by looking at the basics: the MvcControl class.

At first look, this class is supposed to be used as the base of all MVC server controls. The class adds several interesting properties related with MVC:

  • ViewContext: this property goes through the complete control hierarchy until it reaches a valid view (ie, an element of type ViewPage). After that, it simply reaches to the ViewPage’s ViewContext property and caches it in an internal field;
  • ViewDataContainer: this is a property of type IViewDataContainer. In practice, this property will return a reference to the ViewPage or ViewUserControl where the MVC control is defined;
  • ViewData: shortcut for the view data dictionary associated to the current view;

As you’ll see, the other more specialized classes will reuse this info to render the correct HTML (but that’s a story to another post). The class also implements the IAttributeAccessor interface so that it can keep track of the attributes that are defined on the markup (you should do this if you don’t want to map the attributes to properties exposed by the class – like happens in this case). Besides that, the class also overrides the EnableViewState property. It does that in order to hide it from the designer and from the property editor. Unfortunately, it didn’t go as far as it should. In my opinion, it should override the getter and always return false and do nothing on the setter (or throw an exception if true was passed to it). I still have doubts about reusing the Control class,but I guess that it was done for reusing all the parsing logic that exists on the ASP.NET Web Form world.

And that’s it for this post. On the next,we’ll start looking at


~ by Luis Abreu on March 9, 2009.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: