tags:

views:

244

answers:

4

I'm just beginning to learn ASP.NET MVC and I've run into a question. I'm trying to determine whether I should use HtmlHelper to create client controls or if I should just roll my own. My gut wants to lean towards just rolling my own because it gives me total control - and use jQuery to decorate and add cross-browswer functionality. But then I can see advantages of using HtmlHelper for various complex controls that may involve things like paging.

I'm looking for experiences about when it was better to use HtmlHelper and when it was better to roll your own.

+1  A: 

The real question you need to ask yourself is, do you need total control, do you just need a working control that gets the job done in the fastest time possible (this doesn't mean you have to sacrifice quality).

If speed is an issue, use HtmlHelper, if you want to play in control land, then go with rolling your own. If you're not sure, go with HtmlHelper and save homebrewed controls for your spare time experimentation.

TravisO
+2  A: 

Rolling your own is pretty easy for the easy cases, but it gets harder the more "magical" you want them to be.

Luckily you can build the as you go.

The provided HtmlHelper controls are pretty extensive, however. They'll provide most of what you'll need. Just be cautious about the overloads, as it's very easy to accidentally use the wrong overload for your calls.

Ben Scheirman
+2  A: 

The more they add to HtmlHelper the more I end up using them myself.

Take a look at these posts from ScottGu: MVC Preview 5 and MVC Beta

If you don't use the HtmlHelper versions of the input boxes, you won't get the nice, free validation.

Also, I'm not sure what prevents you from decorating the HtmlHelper controls with whatever you need to make them usable for jQuery.

You can add whatever attributes you need using the htmlAttributes overload like so:

<%= Html.TextBox("LastName", ViewData.Model.LastName, new { @class = "required" })%>

The interesting part for what I'm talking about is the anonymous object you see there (new { @class = "required" }).

You can put anything in there you'd need, and it gets slapped onto the attribute section of the control.

Good luck!

CubanX
Thanks, this was very helpful.
Aaron Palmer
A: 

I created a fluent interface for HTML, and I have decided to share it here. Maybe you will find it a good alternative. I also wrote a blog post about it.

Here's a teaser:

<%=this.TextBox(x => x.FirstName).Class("required").Label("First Name:")%>
<%=this.CheckBox("enabled").LabelAfter("Enabled").Title("Click to enable.").Styles(vertical_align => "middle")%>

Critique and contributions are welcome.

Tim Scott