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:
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?
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.
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.
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.
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.
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.
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