【发布时间】:2017-04-02 19:27:22
【问题描述】:
我正在尝试访问模型的元数据属性以创建一个帮助程序,以根据 DataAnnotations 自动添加 HtmlAttributes。
问题是,属性总是空的。
我有 2 个基本课程可以尝试一个空项目:
namespace MegaInterestingProject
{
public class HomeController : Controller
{
public string Index()
{
var model = ModelMetadata.FromLambdaExpression<HomeModel, string>(x => x.User, new ViewDataDictionary<HomeModel>());
return model.Description;
}
}
public class HomeModel
{
[Required]
[MaxLength(13)]
[MinLength(11)]
[DisplayName("displayname")]
[Description("description")]
public string User { get; set; }
}
}
这里model.Description总是空的,AdditionalValues字典总是空的。
这里可能缺少什么?
这是我在 GitHub 上添加的一个参考项目:https://github.com/erickgirard/TestHtmlAttributesHelper
【问题讨论】:
标签: asp.net-mvc data-annotations modelmetadata