Wednesday, January 09, 2008 - Posts

asp.net mvc view user controls

I was having some problems getting the html helper extensions available to a custom view user controls in a asp.net mvc app. I noticed that when using the ide to "add new item" > "mvc view user control", the default behavior is to create a view with code behind. And even though the code behind class inherits from Mvc.ViewUserControl, the html helper extensions weren't available to the user control. After thinking about it for a second, I realized that I'm not planning on using code behind in my view user controls. I'm really just using these to encapsulate some common html, and any data needed will be passed as view data by the page using the control. So, I dropped the code behind and changed my @Control directive to inherit directly from System.Web.Mvc.ViewUserControl and viola, the html helper extensions were now available to the user control. Hmm. Well it works for me, I have no need for the code behind, but if I run into a situation where it becomes necessary I'll need to think about why the html extensions methods dont' appear available with the default code behind/inherit approach.