我正在使用MVC3,并将Entity Framework 4.0实体用作我的模型。 到目前为止,就使用它作为模型而言,一切都很好(所有的crud操作/页面生成都可以直接使用)。 我想知道的是,当您手动生成模型时,如何获得相同的强大标签和验证信息?
这是示例MVC3项目生成的类:
public class LogOnModel
{
[Required]
[Display(Name = "User name")]
public string UserName { get; set; }
[Required]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[Display(Name = "Remember me?")]
public bool RememberMe { get; set; }
}
通过上面的示例,您可以指定为字段呈现什么内容(显示)以及使用哪种类型的字段(密码)。然而,当我尝试使用实体框架并将其推送到下面的视图时,我发现自动生成的标签只是字段名称,并不是我希望用户看到/阅读的任何内容:
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Person</legend>
<div class="editor-label">
@Html.LabelFor(model => model.FirstName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.FirstName)
@Html.ValidationMessageFor(model => model.FirstName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.MiddleName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.MiddleName)
@Html.ValidationMessageFor(model => model.MiddleName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.LastName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.LastName)
@Html.ValidationMessageFor(model => model.LastName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Birthdate)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Birthdate)
@Html.ValidationMessageFor(model => model.Birthdate)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>}
我的问题是:如何为使用EF4生成的实体添加额外的装饰?除了System.ComponentModel.DataAnnotations之外,我还应该使用什么?我知道实体会重新生成,直接向实体代码添加可能不是一个好主意,但出于某种原因,我想不到比在视图中手动输入标签文本更好的方法(这很糟糕,在MVC中不需要这样做!)。 我希望保持应用程序足够动态,能够为我的模型提供正确的显示信息并保持MVC方法。我该怎么做?