我使用ASP.NET MVC 2,使用Visual Studio 2008,並且我的視圖是強類型的。驗證使用ValidationAnnotation
工作。
我試圖找到的是如何在打開表單時啓動驗證。當它打開模型有錯誤,但錯誤不顯示。當我按提交按鈕時,控制器驗證模型並返回到表單。asp.net mvc如何在渲染之前啓動對象驗證
Public Function EditVente(ByVal pNoEnreg As Integer) As ActionResult
Dim dossierVente As VenteDansMedianePlus = model.Helper.selectDossierVente(pNoEnreg)
Return View(dossierVente)
End Function
Public Function enregistrerVente(ByVal pVente As VenteDansMedianePlus) As ActionResult
If ModelState.IsValid Then
model.Helper.updateDossierVente(pVente)
Return RedirectToAction("EditVente", "A009P003", New With {Key .pNoEnreg = pVente.noEnreg})
Else
Return View("EditVente", pVente)
End If
End Function
我試圖把ModelState.IsValid
在editVente
功能,但它不能正常工作。
我的問題是如何在返回視圖之前啓動模型驗證,因此該視圖具有錯誤消息。
我剛剛發現這個http://stackoverflow.com/questions/5347827/validation-messages-are-displayed-when-page-load。那個人有我的解決方案。 – fanfaron