Difference between @Html.Labelfor and @Html.Displaynamefor

Asked

Viewed 2,211 times

1

I am starting at ASP.NET MVC and taking a course where the teacher used to display the data of a different class methods to display a given.

He uses the @Html.LabelFor and the @Html.DisplayNameFor. From what I read on other forums the @Html.LabelFor is used more for creating forms, displays the field contained in a class of a model, but if I use the @Html.DisplayNameFor displays the same data.

1 answer

7


The DisplayNameFor() generates a pure text with the title of that element, already the LabelFor generates a tag label HTML with the title text, thus:

<label for="Campo">Título</label>

What will appear on the page is the same, but the semantics within the page is very different. The second identifies what is that on the page, the first is only a meaningless text for the page/application.

That’s why I say you have to learn what it looks like, not how it looks. It’s the same confusion that people make with "working" and "being right," there’s a lot of stuff that’s wrong and works, and people who learn wrong can’t identify that it’s wrong, because it feels right. But it just seems.

Funcionar é diferente de estar certo

This is the biggest tip you can get in everything. When one does not seek the right information, unlike the AP is doing right and seeking, lives in eternal Dunning Krueger effect.

Both take the title determined by [DisplayName("")] template, or field name if you do not have the specified annotation.

See also.

  • 1

    I would add that both will use the annotaion date [DisplayName("")] of the attribute and if it does not exist, it uses its own name.

  • @Leandroangelo done! Thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.