MVC中ModelState類須要引用 System.Web.Mvc命名空間,在 System.Web.Mvc.dll 中。css
屬性ide
Errors this
返回一個 ModelErrorCollection 對象,該對象包含在模型綁按期間發生的任何錯誤。spa
Valuecode
返回一個 ValueProviderResult 對象,該對象封裝在模型綁按期間綁定的值。orm
Html 輔助方法和ModelState集成對象
HTML 輔助方法,如Html.TextBox(),在輸出內容時,會檢查ModelState集合。若是發現該屬性有異常或錯誤,將呈現用戶輸入的內容和CSS錯誤類。
例如,在Edit視圖中,咱們使用Html.TextBox() 輔助方法呈現Dinner對象的EventDate屬性:
<%= Html.TextBox("EventDate", String.Format("{0:g}", Model.EventDate)) %>
當有錯的時候呈現視圖時,Html.TextBox() 方法檢查ModelState集合,檢查是否有錯誤關聯到Dinner 對象的EventDate屬性。當發現有錯誤時,將顯示用戶提交的」EntLib」 輸入做爲參數值,同時對<input type=」textbox」 />元素添加CSS 錯誤類,以下所示:
<input class="input-validation-error" id="EventDate" name="EventDate"
type="text" value="BOGUS" />
你能夠定製CSS錯誤類的樣式。默認的CSS錯誤類 – input-validation-error定義在\content\site.css 文件中,樣式定義以下:
.input-validation-error
{
border: 1px solid #ff0000;
}
Html.ValidationMessage() 輔助方法
Html.ValidationMessage() 輔助方法用來輸出特定Model屬性相關的ModelState錯誤信息:
<%= Html.ValidationMessage("EventDate") %>
上述代碼輸出:
<span class=」field-validation-error」> The value ‘EntLib’ is invalid</span>
Html.ValidationMessage() 輔助方法也支持第二個參數,容許開發人員覆蓋錯誤消息:
<%= Html.ValidationMessage("EventDate", "*") %>
上述代碼輸出:
<span class=」field-validation-error」> *</span>,而不是默認的錯誤信息。
Html.ValidationSummary() 輔助方法
Html.ValidationSummary() 輔助方法將呈現總結的錯誤消息,經過<ul><li/></ul>元素列出在ModelState集合中全部詳細的錯誤消息
Html.ValidationSummary() 輔助方法接收一個可選的字符串參數 – 定義一個歸納性的錯誤消息,並顯示在全部詳細錯誤信息的前面:
<%= Html.ValidationSummary("Edit was unsuccessful. Please correct the errors and try again.") %>
你也能夠定義CSS設置錯誤消息的樣式。
使用AddRuleViolations輔助方法
初始的HTTP-POST Edit的實現方法使用了一個foreach循環語句,遍歷Dinner對象的Rule Violations,並添加到controller的ModelState集合:
catch
{
foreach (var issue in dinner.GetRuleViolations())
{
ModelState.AddModelError(issue.PropertyName, issue.ErrorMessage);
}
return View(dinner);
}
爲了使代碼更簡潔一點,咱們添加ControllerHelpers類到NerdDinner項目中,並實現了AddRuleViolations擴展方法,添加了一個對ASP.NET MVC ModelStateDictionary 類的輔助方法。該擴展方法封裝了使用RuleViolation 錯誤信息填充ModelStateDictionary 集合類的邏輯:
public static class ControllerHelpers
{
public static void AddRuleViolations(this ModelStateDictionary modelState,
IEnumerable<RuleViolation> errors)
{
foreach (RuleViolation issue in errors)
{
modelState.AddModelError(issue.PropertyName, issue.ErrorMessage);
}
}
}
接下來,咱們更新HTTP-POST Edit方法,使用上述擴展方法實現Dinner的Rule Violations填充ModelState集合。
完成Edit Action方法的實現
下面的代碼實現了控制器中Edit的全部邏輯:

//
// GET: /Dinners/Edit/2
public ActionResult Edit(int id)
{
Dinner dinner = dinnerRepository.GetDinner(id);
return View(dinner);
}
//
// POST: /Dinners/Edit/2
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Edit(int id, FormCollection formValues)
{
Dinner dinner = dinnerRepository.GetDinner(id);
try
{
UpdateModel(dinner);
dinnerRepository.Save();
return RedirectToAction("Details", new { id = dinner.DinnerID });
}
catch
{
ModelState.AddRuleViolations(dinner.GetRuleViolations());
return View(dinner);
}
}

關於Edit方法的實現的優勢,不只Controller類,並且View視圖模板都沒必要關心Dinner模型類的特定驗證方法或者業務規則。之後,咱們能夠針對Model類增長額外的業務規則,而沒必要要求Controller和View更改代碼。這樣,咱們能夠根據需求,以最小的更改代碼量,靈活改進應用程序。