What is the advantage of using Tag Helpers in ASP.Net MVC 5
-
apologized that i am not very good in asp.net mvc that i would like to confess. i just come across a good write up for asp.net 5 new feature from this url http://stephenwalther.com/archive/2015/02/24/top-10-changes-in-asp-net-5-and-mvc-6 from there i heard about a term called Tag Helpers in ASP.Net MVC 5 and i saw there people say before developer create form this below way
@model MyProject.Models.Product
@using (Html.BeginForm())
{@Html.LabelFor(m => p.Name, "Name:") @Html.TextBoxFor(m => p.Name) <input type="submit" value="Create" />
}
and now people can code the same with tag helper the below way
@model MyProject.Models.Product
@addtaghelper "Microsoft.AspNet.Mvc.TagHelpers"<form asp-controller="Products" asp-action="Create" method="post">
<label asp-for="Name">Name:</label> <input asp-for="Name" /> <input type="submit" value="Save" />
</form>
they use some few new syntax called asp-controller,asp-for etc but what it will do.....how and why people would be benefited by using this new syntax asp-controller,asp-for etc. so please some one help me to understand this new tag helper concept and how it will make a developer life easier. thanks
tbhattacharjee
-
apologized that i am not very good in asp.net mvc that i would like to confess. i just come across a good write up for asp.net 5 new feature from this url http://stephenwalther.com/archive/2015/02/24/top-10-changes-in-asp-net-5-and-mvc-6 from there i heard about a term called Tag Helpers in ASP.Net MVC 5 and i saw there people say before developer create form this below way
@model MyProject.Models.Product
@using (Html.BeginForm())
{@Html.LabelFor(m => p.Name, "Name:") @Html.TextBoxFor(m => p.Name) <input type="submit" value="Create" />
}
and now people can code the same with tag helper the below way
@model MyProject.Models.Product
@addtaghelper "Microsoft.AspNet.Mvc.TagHelpers"<form asp-controller="Products" asp-action="Create" method="post">
<label asp-for="Name">Name:</label> <input asp-for="Name" /> <input type="submit" value="Save" />
</form>
they use some few new syntax called asp-controller,asp-for etc but what it will do.....how and why people would be benefited by using this new syntax asp-controller,asp-for etc. so please some one help me to understand this new tag helper concept and how it will make a developer life easier. thanks
tbhattacharjee
Hi, This is an attempt to make you feel that you are not doing mixed coding (Client side and server side). Also, it may reduce the context switching (IMHO). Moreover, tag based syntax is good for simplicity. Just think if you have to add 10 attributes in your html helper. Your code becomes un-readable. With tag helper, your server side and client side tags and attributes will go hand in hand with much improved readability.
Life is a computer program and everyone is the programmer of his own life.
-
Hi, This is an attempt to make you feel that you are not doing mixed coding (Client side and server side). Also, it may reduce the context switching (IMHO). Moreover, tag based syntax is good for simplicity. Just think if you have to add 10 attributes in your html helper. Your code becomes un-readable. With tag helper, your server side and client side tags and attributes will go hand in hand with much improved readability.
Life is a computer program and everyone is the programmer of his own life.
thanks for your reply. can you please discuss the advantage with sample code whatever you said in your answer. if u plzz explain with small small sample code then i may understand. thanks
tbhattacharjee
-
thanks for your reply. can you please discuss the advantage with sample code whatever you said in your answer. if u plzz explain with small small sample code then i may understand. thanks
tbhattacharjee
I would recommend you to visit http://asp.net/mvc and other websites which talk about it. You will get many examples there. Please don't hesitate to ask if you have specific pin-point question. TagHelpers[^]
Life is a computer program and everyone is the programmer of his own life.
-
thanks for your reply. can you please discuss the advantage with sample code whatever you said in your answer. if u plzz explain with small small sample code then i may understand. thanks
tbhattacharjee
Your sample code has a perfect example. So as it is now you have a form like so
@model MyProject.Models.Product
@using (Html.BeginForm())
{@Html.LabelFor(m => p.Name, "Name:") @Html.TextBoxFor(m => p.Name) <input type="submit" value="Create" />
}
Just by looking at this code I don't really know where the form is going to be posted to. Also your standard HTML controls are created using specific C# HTML Helpers, which can make the html a little confusing for some. Now taking a look at the code that uses Tag Helpers, the HTML is much more readable.
@model MyProject.Models.Product
@addtaghelper "Microsoft.AspNet.Mvc.TagHelpers"<form asp-controller="Products" asp-action="Create" method="post">
<label asp-for="Name">Name:</label> <input asp-for="Name" /> <input type="submit" value="Save" />
</form>
I know exactly where this form is going to post back to. And I also know where my HTML elements are and where my C# code is. For me, it makes the view a whole lot easier to read and understand.
-
apologized that i am not very good in asp.net mvc that i would like to confess. i just come across a good write up for asp.net 5 new feature from this url http://stephenwalther.com/archive/2015/02/24/top-10-changes-in-asp-net-5-and-mvc-6 from there i heard about a term called Tag Helpers in ASP.Net MVC 5 and i saw there people say before developer create form this below way
@model MyProject.Models.Product
@using (Html.BeginForm())
{@Html.LabelFor(m => p.Name, "Name:") @Html.TextBoxFor(m => p.Name) <input type="submit" value="Create" />
}
and now people can code the same with tag helper the below way
@model MyProject.Models.Product
@addtaghelper "Microsoft.AspNet.Mvc.TagHelpers"<form asp-controller="Products" asp-action="Create" method="post">
<label asp-for="Name">Name:</label> <input asp-for="Name" /> <input type="submit" value="Save" />
</form>
they use some few new syntax called asp-controller,asp-for etc but what it will do.....how and why people would be benefited by using this new syntax asp-controller,asp-for etc. so please some one help me to understand this new tag helper concept and how it will make a developer life easier. thanks
tbhattacharjee
Apart from already explained answer, on a side note just want to clarify that it is for Asp.Net MVC 6. Not MVC 5. I know the version number of Asp.Net, C#, MVC and Visual Studio often confuse people. :) Next Release versions: Asp.Net: 5 Asp.Net MVC: 6 C#: 6 Visual Studio: 2015
Life is a computer program and everyone is the programmer of his own life.