Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC 3 Razor - Trigger validation from controller

I have to check to see if the new users email already exists in the database. The email passes all the normal validation but what if I want to trigger a special validation from the controller if the email already exists after checking it against the database?

like image 580
RichC Avatar asked Apr 03 '11 02:04

RichC


2 Answers

In controller: ModelState.AddModelError("ErrorEmail", "Error Message");

In View: @Html.ValidationMessage("ErrorEmail")

Hope this helps

like image 117
ysrb Avatar answered Nov 17 '22 06:11

ysrb


I think what you are looking for is the RemoteAttribute.

This is a ValidationAttribute for remote validation. It works like the other validation attributes by adding model errors to your modelstate dictionary.

Check out these articles on using the RemoteAttribute:

  • http://deanhume.com/Home/BlogPost/mvc-3-and-remote-validation/51
  • http://davidhayden.com/blog/dave/archive/2011/01/04/ASPNETMVC3RemoteValidationTutorial.aspx
like image 33
smartcaveman Avatar answered Nov 17 '22 06:11

smartcaveman