Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the correct Tag usage in ASP.net Core? TagHelpers vs HTML Helper

Since the release of ASP.Net Core, Asp team is pushing the use of TagHelpers more than the HTML helpers.

While scaffolding view from controller, ASP.Net Core creates add, edit page with the use of TagHelpers, But if you notice the index page they use old HTML helpers.

//Ex:
    <th>
        @Html.DisplayNameFor(model => model.Package)
    </th>

It is very confusing to understand what the ASP.net Core Teams agenda. Does anybody know which is correct way of doing this in ASP.net Core? My guess is that @Html.DisplayNameFor(model => model.Package) will be marked as deprecated soon. Please explain?

like image 233
Kirk Avatar asked Sep 29 '16 07:09

Kirk


3 Answers

Not every Html helper has an equivalent Tag helper. For instance, there is no equivalent Tag helper for @Html.DisplayNameFor(m => m.Property) or @Html.EditorForModel(). It makes sense because @Html.DisplayNameFor(m => m.Property) and @Html.EditorForModel() doesn't really output an HTML tag.

So use Tag helpers in favor of Html helpers if there is an equivalent. Otherwise, use Html helpers.

I don't think @Html.DisplayNameFor(m => m.Property) will be marked as deprecated (nor will any other display templates or editor templates such as @Html.EditorForModel()). These will continue to be used in ASP.NET Core.

Also, you might be interested in this thread regarding a TagHelper equivalent to Html.DisplayNameFor: https://github.com/aspnet/Mvc/issues/2387

like image 119
kimbaudi Avatar answered Sep 22 '22 10:09

kimbaudi


According to that thread,

https://github.com/aspnet/Mvc/issues/3003

it seems like for the moment DisplayNameFor is here to stay and that there is no Tag Helper for your solution.

They mention <label asp-for="blabla"></label> a lot but this is not the same.

like image 24
Darxtar Avatar answered Sep 22 '22 10:09

Darxtar


I think tag helpers is the way to go, it is more readable, cleaner and can be used to create more complex tags that simulate the user controls in web forms days

like image 33
Haitham Shaddad Avatar answered Sep 20 '22 10:09

Haitham Shaddad