我正在使用MVC3,並使用Entity Framework 4.0實體作爲我的模型。到目前爲止,只要將它用作模型(所有的crud操作/頁面生成都是現成的),一切都很有效。不過,我想知道如何獲得與手動生成模型相同的健壯標籤和驗證信息?使用System.ComponentModel.DataAnnotations與實體框架4.0
下面是我的意思的例子。這是樣品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方法。我該怎麼做?
相關:http://stackoverflow.com/q/2999936/11912 – 2013-03-27 14:12:33