MVC4: manually set the verification message from the server

I want to check some form fields on the server side, but I do not want to use custom Annotations data validators. I need to manually set its value based on the return of the called business layer method to determine this message.

As an example!

NECESSARY:

If the specified username already exists, the MVC4 validation field should display "This username already exists."

CODE:

 if (_business.UserNameExists(username))
 {
    // Set the field validation error span message
    // HOW TO DO??
 }
+5
source share
1 answer

A friend came up with a solution, it is very simple!

 if (_business.UserNameExists(username))
 {
    // Set the field validation error span message
    ModelState.AddModelError("UserName", "This username already exists.");
 }

Where UserName is the name of the Entity attribute to check.

+14
source

All Articles