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?
Tag Helpers are attached to HTML elements inside your Razor views and can help you write markup that is both cleaner and easier to read than the traditional HTML Helpers. HTML Helpers, on the other hand, are invoked as methods that are mixed with HTML inside your Razor views.
Helper class can create HTML controls programmatically. HTML Helpers are used in View to render HTML content. It is not mandatory to use HTML Helper classes for building an ASP.NET MVC application. We can build an ASP.NET MVC application without using them, but HTML Helpers helps in the rapid development of a view.
The HtmlHelper class renders HTML controls in the razor view. It binds the model object to HTML controls to display the value of model properties into those controls and also assigns the value of the controls to the model properties while submitting a web form.
"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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With