MVC 4 Data Annotations “Display” Attribute

后端 未结 4 1363
夕颜
夕颜 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-01 01:38

    In addition to the other answers, there is a big benefit to using the DisplayAttribute when you want to localize the fields. You can lookup the name in a localization database using the DisplayAttribute and it will use whatever translation you wish.

    Also, you can let MVC generate the templates for you by using Html.EditorForModel() and it will generate the correct label for you.

    Ultimately, it's up to you. But the MVC is very "Model-centric", which is why data attributes are applied to models, so that metadata exists in a single place. It's not like it's a huge amount of extra typing you have to do.

提交回复
热议问题