【发布时间】:2010-11-29 20:44:14
【问题描述】:
为什么 DataAnnotations 不适用于公共字段?示例:
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)
如果 Title 是一个字段,那么 Display 属性似乎没有任何作用。如果将 Title 更改为属性,它会按预期工作,如显示“Name”。
在此示例中,仅更改为属性似乎很容易,但我正在尝试使用 F# 中的类型,将它们编译为具有字段而不是属性的类。
这是在 ASP.NET 4 和 MVC RC 3 中测试的。
【问题讨论】:
标签: asp.net-mvc data-annotations