DataAnnotations on public fields vs properties in MVC

后端 未结 1 1549
我在风中等你
我在风中等你 2021-01-06 11:13

Why don\'t DataAnnotations work on public fields? Example:

namespace Models
{
    public class Product
    {
        [Display(Name = \"Name\")]
        publi         


        
1条回答
  •  情书的邮戳
    2021-01-06 11:27

    The reason why DataAnnotations do not work with fields is because the reflection-like mechanism that is used to retrieve the attributes (TypeDescriptor) only supports properties.

    While it would not be easy, we could look into making this work with fields if there is enough demand.

    0 讨论(0)
提交回复
热议问题