Should I be using HtmlHelper or a roll of my own controls?

I am just starting to learn ASP.NET MVC and I have a question. I am trying to determine if I should use HtmlHelper to create client controls or if I should just roll my own. My gut wants to bend over just to roll on its own because it gives me full control - and use jQuery to decorate and add cross-browser functionality. But then I can see the benefits of using the HtmlHelper for various complex controls that can involve things like paging.

I'm looking for experience when it was better to use HtmlHelper and when it was better to roll.

+1


source to share


4 answers


The more they add to the HtmlHelper, the more I use them myself.

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

Unless you're using the HtmlHelper versions for input fields, you won't get a good free validation.

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

You can add all the required attributes using the htmlAttributes overload, for example:



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

      

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

You can put whatever you want and it will be removed under the attribute section of the control.

Good luck!

+3


source


Rolling yours is pretty easy for simple cases, but gets harder than the "magical" ones you want them to be.

Fortunately, you can build as you go.



The provided HtmlHelper controls are quite extensive. They will provide most of what you need. Just be careful about overloads, as it is very easy to accidentally use the wrong overload for your calls.

+2


source


The real question you need to ask yourself is: you need total control, you just need a worker control that gets the job done as quickly as possible (that doesn't mean you have to sacrifice quality).

If speed is an issue, use HtmlHelper if you want to play in the control area, then move on to your own. If you're unsure, go with the HtmlHelper and save the homebrew controls for your free time.

+1


source


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

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")%>

      

Criticism and contributions are welcome.

0


source







All Articles