开发者

MVC Form Validation

开发者 https://www.devze.com 2023-03-23 18:44 出处:网络
I have a form that I would like to validate that a field exist in a database. I am currently validating that all fields are in the correct text format etc and posting them to the controller.

I have a form that I would like to validate that a field exist in a database. I am currently validating that all fields are in the correct text format etc and posting them to the controller.

The problem being that should the field not exist in the database I am returning the view but the post data is being lost from the form and I am also unaware of how to throw a nice error message explaining what has just happened.

Would anyone know of nice method for preforming some validation on form data within th开发者_开发技巧e controller and throwing exceptions messages all while maintaining the form data in the form?


Solve this by simply adding errors to the modelstate stack:

ModelState.AddModelError("_FORM", "The new password and confirmation password do not match.");

Simples!

0

精彩评论

暂无评论...
验证码 换一张
取 消