Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are ASP.NET MVC HTML Helpers overrated?

It is quite possible that I may not have got the point, but I really can't figure out how ASP.NET MVC's HTML Helpers can help me. Here's a sample: -

HTML:

<a href="ActionName" target="_blank">Click Me</a>

HTML Helper:

<%= Html.ActionLink("Click me", "ActionName", null, new {target="blank"}) %>

My eyes are reading HTML more easily, and it seems counter-intuitive to use the HTML Helpers.

Look at the following arguments:

  • A lot of people (even novices) know how to read HTML. The HTML Helper syntax can confuse easily.
  • In many cases you need to do more typing writing an HTML 'helper', then you need to write the actual HTML.
  • The HTML Helper is going to spew real HTML anyway, so why not write HTML directly?
  • Writing HTML gives you more control over the syntax, and the standards. You can make it conform to whatever HTML standard you want.

Are there any special compelling reasons that I have not understood (since I am self-educated in MVC and there could be gaps) that should make me want to prefer HTML Helpers?

Or are they just code noise?

like image 543
Cyril Gupta Avatar asked Aug 29 '09 00:08

Cyril Gupta


People also ask

Should I use tag helpers or HTML helpers?

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.

Why we use HTML helpers in MVC?

In MVC, HTML Helper can be considered as a method that returns you a string. This string can describe the specific type of detail of your requirement. Example: We can utilize the HTML Helpers to perform standard HTML tags, for example HTML<input>, and any <img> tags.

What is strongly typed HTML helpers in MVC?

The Strongly-Typed HTML helper (i.e., NumericTextBox) takes lambda as a parameter that tells the helper, which element of the model to be used in the typed view. The Strongly typed views are used for rendering specific types of model objects, instead of using the general ViewData structure.

Where do we use HTML helpers?

An HTML Helper is just a method that returns a HTML string. The string can represent any type of content that you want. For example, you can use HTML Helpers to render standard HTML tags like HTML <input>, <button> and <img> tags etc.


1 Answers

The primary reason you don't use <a> tags directly is that you don't want to hardcode URLs in your application. The Html.ActionLink method will abstract away the URL generation and you'll specify the controller, action, and other parameters.

So, basically, the two lines you posted in your question are not really equivalent. You should consider adding the dynamic URL generation code to the <a> tag to make them functionally equivalent. Beside that, if you output HTML directly, you'll have to be extremely careful about HTML encoding stuff. Html.ActionLink will do this job for you too.

like image 148
mmx Avatar answered Sep 28 '22 20:09

mmx