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?
相关问题
- MVC-Routing,Why i can not ignore defaults,The matc
- Date with SimpleDateFormat in Java
- parameters in routing do not work MVC 3
- There is no ViewData item with the key 'taskTy
- TextBoxFor decimal
相关文章
- Angular Material Stepper causes mat-formfield to v
- Why doesn't Django enforce my unique_together
- How to get a list of connected clients on SignalR
- How do you redirect to the calling page in ASP.NET
- Change color of bars depending on value in Highcha
- The program '[4432] iisexpress.exe' has ex
- ASP.Net MVC 4 Bundles
- How to get server path of physical path ?
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
:In controller:
ModelState.AddModelError("ErrorEmail", "Error Message");
In View:
@Html.ValidationMessage("ErrorEmail")
Hope this helps
I found a way to perform conditional validation from the ViewModel. The VM class will need to implement the IValidatableObject interface.
Then add a method similar to this at the bottom of the VM:
And of course you will need this in the View:
Hope that helps!