30

I understand the reason for having the HTML helpers in ASP.NET MVC and extending this to provide your own, but I am wondering whether using HTML helpers is a good idea.

I thought one of the benefits of ASP.NET MVC is control over the HTML. If you start hiding it away in helper functions that generate HTML don't you start losing visibility? I guess this isn't such a problem when you are generating simple controls such as a button, but I have seen the use of html helpers to create grids and more complex HTML output.

Now I also understand the reason for doing so is to keep things DRY, avoiding duplication. But is there not a danger of having something akin to code-behind here? In addition, what if you are working in collaboration with designers? Generally the designer would be creating the markup and applying styling. If you start injecting your view with helpers that generate markup, doesn't this make such collaboration difficult?

1
  • No. As long as helpers are not violating the concerns of MVC, that is they're doing something that is not presentation logic, then there is nothing wrong with them. Yes, some complex helpers may in fact be violating the separation of concerns, but that's really a different argument from whether or not helpers themselves are good or bad. Commented May 19, 2013 at 7:16

4 Answers 4

16

"Control over the HTML" is microsoft marketing-speak, and is how they are choosing to brand the platform. The point of ASP.net MVC is that it is a more simple, and a better fit for webapps then the whole stateful event-driven model of webforms, and something that pretty much everyone outside of the microsoft space moved to years ago. Microsoft can't say that though, because they have a huge investment in webforms, and it is a key part of their enterprise story.

That being said, if you have business logic in your helpers you are using them wrong. It is basically code behind for presentation logic only that is duplicated across multiple pages, and the goal is to keep scriptlet tags in the markup as simple as possible.

As long as you use the helpers the way they should be used, it should be fairly trivial for the designers to learn how to use. Just remember that the goal is to keep things simple, if they end up making things more complex, it means they are not being used correctly.

Sign up to request clarification or add additional context in comments.

Comments

9

Great comment, Matt. There's still the question of whether in a "pure" MVC implementation, HTML helpers are a good idea. I think that's the magic word, "pure". Whenever I slow down to think about the "proper" way of doing things, I'm really trying to see if a particular approach fits in with the purist's vision. So, would a purist use HTML helpers?

I'm about 8/10 purist and I wouldn't use them. I've seen this argument transcend technologies and raised the issue with MVC in PHP and the Zend framework. It just doesn't feel right and to me, that's the best measure one can come up with.

1 Comment

There is nothing in the MVC pattern that says you can't use a helper function to generate template markup code. MVC talks only about the separation of concerns. So long as your helper functions are not violating the boundaries of concerns, then MVC has nothing to say about it. As such "pure" MVC from a strictly pattern standpoint doesn't come into it, as there is nothing that deals with any of the aspects of MVC. These helpers are purely a feature of the view (the V in MVC).
3

There's definitely nothing wrong with helpers. They're used to keep your views clean and declarative. There's a saying that goes something like "if there's an "if" statement in your view, you're doing it wrong". They're used in many prominent MVC frameworks like Ruby On Rails and Cake PHP. Check out this post. "Purist" or not, helpers are a good thing and not to be confused with bad practice or leaky abstractions.

Comments

1

I think one important point that the others haven't mentioned is portability of your views.

You can move your HTML, javascript and CSS to an application on another platform immediately. You don't have to convert all the ugly HTMLHiders.. sorry, HTMLHelpers.. to actual HTML.

While I have the highest regard for the .NET framework that reduces my development time and makes my job easy, I strongly feel that views should be non-proprietary.

You can gain almost all of the benefit from the framework in the model and controller anyway. Injecting dependencies on the framework in your presentation layer is not at all worth the tradeoff, IMO.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.