DisplayNameFor and DisplayFor

2

Studying ASP.NET MVC, I came across the following lines of code:

@Html.DisplayNameFor(model => model.Title)
@Html.DisplayFor(model => model.Title)

I can not understand the difference between using DisplayNameFor and DisplayFor .

    
asked by anonymous 08.08.2017 / 19:21

2 answers

4
@Html.DisplayNameFor(model => model.Title) //mostra Title
@Html.DisplayFor(model => model.Title) //mostra o conteúdo de Title

You almost always want to use DisplayFor .

Actually the DisplayNameFor "shows the name of the property that can be the name that was declared or can use a different name annotated with an attribute. It may be useful when you want to show a description of what the content is. As we do not speak English and do not use mnemonics almost always the name of the property is not adequate. But if you have a note of the name then it can work as a label for the content. Some people prefer to have a separate label or defined in view as it can have a context. Someone prefers to have this binding of the property with their name through Display() .

To annotate a name you can do so in the template:

public class Livro {
    [Display(Name = "Tíulo do livro")]
    public string Title{ get; }
}

It's very much like the LabelFor .

    
08.08.2017 / 19:31
3

I followed this example :

public class Teste
{
    [Display(Name = "current name")]
    public string Nome { get { return "teste"; } }
}
Using @Html.DisplayNameFor(model => model.Nome) would display the name of the Nome property or (in this case) the description placed in the Display (current_name) property. @Html.DisplayFor(model => model.Title) displays property value ( test )

    
08.08.2017 / 19:28