Aswajith
Aswajith

Reputation: 9387

How do Tag Helpers differ from the Helper class?

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

Answers (2)

Phil
Phil

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

Matt DeKrey
Matt DeKrey

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:

Front-end developers do not need knowledge of C# to write them.

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.

It makes it easier to add other attributes, such as 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" />

You can even write your own.

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

Related Questions