DataAnnotations in public fields and MVC properties

Why don't DataAnnotations work in open fields? Example:

namespace Models
{
    public class Product
    {
        [Display(Name = "Name")]
        public string Title; // { get; set; }
    }
}

public ActionResult Test()
{
     return View(new Models.Product() { Title = "why no love?" });
}

@Html.LabelFor(m => m.Title) // will return 'Title' if field, or 'Name' if property
@Html.DisplayFor(m => m.Title)

If Title is a field, the Display attribute seems invalid. If the Title is changed to a property, it works as expected, "Name" is displayed.

It would seem that in this example it’s easy to just change the property, but I'm trying to use types from F # where they are compiled into a class with fields, and not with properties.

This has been tested in ASP.NET 4 and MVC RC 3.

+3
source share
1 answer

, DataAnnotations , , , (TypeDescriptor), .

, , .

+2

Source: https://habr.com/ru/post/1777532/


All Articles