efavogslid
New Member
I am starting out with MVC 4 (Razor view engine). (I believe this may apply to MVC 3 and earlier as well.) I am wondering if there is any benefit to using the DisplayAttribute data annotation within a view versus just writing a string directly in the HTML. For example, if I had the following model:\[code\]public class Thing{ public string WildAndCrazyProperty { get; set; }}\[/code\]...would there be any benefit in annotating the property as:\[code\] [Display(Name = "Wild and Crazy")] public string WildAndCrazyProperty { get; set; }\[/code\]...and having my markup be:\[code\]<html> <body> <div>@Html.DisplayNameFor(modelItem => modelItem.WildAndCrazyProperty)</div> <div>@Html.DisplayFor(modelItem => modelItem.WildAndCrazyProperty)</div> </body></html>\[/code\]...versus not having the annotation, and doing:\[code\]<html> <body> <div>Wild and Crazy</div> <div>@Html.DisplayFor(modelItem => modelItem.WildAndCrazyProperty)</div> </body></html>\[/code\]The reason I haven't mentioned \[code\]Html.LabelFor\[/code\] in this case is because the property's data is being displayed as static (i.e. non-editable) text on the page. The data will never be editable on this page, so there is no need for me to use \[code\]Html.TextBoxFor\[/code\] within the second <div> and subsequently use the \[code\]Html.LabelFor\[/code\] to properly associate a label with that text box.