MVC 4 Data Annotations “Display” Attribute

后端 未结 4 1273
夕颜
夕颜 2021-02-01 01:19

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 d

4条回答
  •  北恋
    北恋 (楼主)
    2021-02-01 01:45

    One of the benefits is you can use it in multiple views and have a consistent label text. It is also used by asp.net MVC scaffolding to generate the labels text and makes it easier to generate meaningful text

    [Display(Name = "Wild and Crazy")]
    public string WildAndCrazyProperty { get; set; }
    

    "Wild and Crazy" shows up consistently wherever you use the property in your application.

    Sometimes this is not flexible as you might want to change the text in some view. In that case, you will have to use custom markup like in your second example

提交回复
热议问题