The MVC platform – the HtmlHelper class (part I)
After a long break, it’s time to go back to the MVC framework study. And today I’ll start from where I’ve stopped last time: the helpers. Today, I’m going to start talking about the HtmlHelper class which has lots of methods that you can use on your views for generating HTML. If you’ve downloaded the source code and then look at the HtmlHelper.cs file, you’ll be able to see that most of the methods you’ll discover through intellisense are defined by extension methods. Today I won’t talk about these methods. Instead, I’m going to concentrate on the methods defined directly on the class (ie, on the methods that are defined on the body of the class).
By default, the HtmlHelper exposes only a handful of properties for giving you access to:
- the data dictionary that is passed to the view (ViewData property);
- to the current route collection (RouteCollection property);
- to the current context (ViewContext property);
- to the current IViewDataContainer element (see more about this interface in this post).~
Besides these properties, the class exposes several utility methods. Many of these could be exposed as static methods but the team decided to expose them as instance methods in order to maintain consistency across the API. Here’s a summary of the instance methods you’ll find in the class:
- AttributeEncode: used for encoding the values that you intend to pass to an attribute. There are two overloads of this method (the one that receives an Object will call the Convert.ToString method before passing the value to the HttpUtility.HtmlAttributeEncode method);
- Encode: similar to the previous method, but in this case you’ll end up using the HttpUtility.Encode method (remember that the HttpUtility.HttpAttributeEncode should only be used for double quoted attribute values);
- EvalString/EvalBoolean: as you can see from their names, these methods are responsible for receiving a string that identifies an item on the ViewDataDictionary and for returning it to you (already casted to string or boolean).
Besides these instance methods, you’ll also find several static methods. For instance, there’s a GetFormMethodString which transforms a value from the FormMethod enumerations into a string (I’m not sure on why this method is here or why it is needed at all since you’re only using it from the Form method helper – more about this on future posts).
There are also another couple of internal methods:
- GetModelAttemptedValue: method used for getting a value from the ModelState dictionary available from the ViewData property (if I recall it correctly, ModelState was introduced in preview 5 for helping in validation of form items – more about this on a future post);
- FindPartialView: auxiliary method used for getting a reference to a view (used by the RenderPartialinternal method for getting a reference to an IView object that is responsible for rendering the HTML sent back to the client);
- RenderPartialInternal: used internally by the views for rendering partial views (check this post for more info).
And that’s all you have defined on the body of the class. There’s a lot more (under the form of extension methods), but we’ll leave it for a future post. Keep tuned!