我試圖找出驗證單頁結帳的最佳方法。 它包含:地址字段的自定義ASP.NET MVC驗證摘要
- 船地址
- 帳單地址
- 等
Address類明顯包含First Name
,Last Name
,Street1
,Street2
,City
,State
,Zip
,Phone
等。
比方說,用戶在輸入任何內容之前點擊「確定」 - 然後最終出現十幾個驗證錯誤,給你一大塊看起來很醜的紅色文本。
我想驗證的地址作爲一個單一的實體,並給出一個智能錯誤 - 在適當的時候,例如「不完整的地址」,或更具體的錯誤。但我仍然希望能夠突出顯示每個有問題的單個領域。我現在看不到一個簡單的方法,因爲顯然Html.ValidationSummary
幫手會顯示每個字段。
所以我想說明的總結爲:紅色Zip
和City
"Your shipping address is incomplete"
和亮點。
我想我必須做一個完全自定義的ValidationSummary,甚至可能是一個完全自定義的數據結構。
做任何驗證框架都容易這樣的總結做,在總結應該表現出一種智能總結並不僅僅是每一個人領域的錯誤。
編輯:MVC 2 RC現在支持模型級的錯誤。
ValidationSummary now supports overloads where only model-level errors are displayed. This is useful if you are displaying validation messages inline next to each form field. Previously, these messages would be duplicated in the validation summary. With these new changes, you can have the summary display an overall validation message (ex. 「There were errors in your form submission」) as well as a list of validation messages which don’t apply to a specific field.
任何人有如何做到這一點的實際樣本?
但隨後各個字段不會突出顯示。我不想失去視覺提示 - 尤其是因爲並非所有的字段實際都是必需的(比如Address2) – 2009-11-29 19:30:46
@Simon:如果你想讓字段突出顯示,你可以添加空的錯誤:if(String.IsNullOrEmpty(address.Zip ))ModelState.AddModelError(「Zip」,「」);'它不會顯示在驗證摘要中。 – LukLed 2009-11-29 20:32:40