Reputation: 9387
ASP.Net MVC 6 introduced Tag helpers feature.
Why should we use tag helpers over the Helper class provided by previous versions?
Upvotes: 4
Views: 373
Reputation: 2181
IMHO, the big reason is that it makes your markup cleaner; looking less like server-side code and more like HTML5.
Upvotes: 0
Reputation: 11942
The Tag Helpers were introduced for several reasons. Scott Hanselman talks in length about them here. However, the points for Tag Helpers include:
The following is really not very intuitive for a traditional front-end developer.
@Html.LabelFor(m => m.Title, new { @class = "control-label" })
@Html.TextBoxFor(m => m.Title, new { @class = "form-control" })
However, it gets much more clear using tag helpers:
<label asp-for="Title" class="control-label" />
<input asp-for="Title" class="form-control" />
Note that this was in flux last I checked, including whether or not to use the asp-
prefix.
class
, placeholder
, or even other custom attributes.I use angular.js with my projects; for this, the syntax becomes even more confusing:
@Html.TextBoxFor(m => m.Title, new { @class = "form-control", ng_model = "ctrl.title" })
Now it's as simple as adding the attribute, like we'd expect:
<input asp-for="Title" class="form-control" ng-model="ctrl.title" />
I'm not going to go in depth here, but you can create server-side expanding widgets that can even match your client-side frameworks for SEO purposes. There's a pretty decent tutorial for making tag helpers.
Upvotes: 4