Custom validation error not displayed

I have an MVC 3 application and am trying to display a custom validation error. The normal validation errors that are generated by the model, i.e. Required, are displayed on the page. Now I am checking if a user exists and if so, adding a error message:

if (userExists)
    ModelState.AddModelError("UserName", UserManagementResources.UserAlreadyExistsText);
return View(model);

On the view I have a validation summary and a Html.ValidationMessage("UserName"), but neither one is displaying the error. I have used this successfully on other pages. The only difference with this page I can see is, that it uses the RequiredIf validator scripts.

Any ideas how to solve this problem are appreciated. Thanks.


I am returning the validation message through the Remote validation. If I look what the network is doing, it's returning the error message, but it is still not displayed on the view.

    [Remote("IsUserAvailable", "Validation", ErrorMessage = "Ein Benutzer mit dieser Email existiert bereits.")]
    [Display(Name = Resources.EmailText, ResourceType = typeof(Resources))]
    public string Email
        get { return User.Email; }
        set { User.Email = value; }

The View:

@Html.LabelFor(u => u.Email, Resources.Email + " (Login) *")
@Html.EditorFor(u => u.Email)
@Html.ValidationMessageFor(u => u.Email)
<br clear="all" />

The Remote Validation Controller:

public class ValidationController : Controller
    public JsonResult IsUserAvailable(string Email)
        bool userExists;

        using (var userModel = new UserManagementModel())
            userExists = userModel.UserExists(Email);

            return Json(UserManagementResources.UserAlreadyExists, JsonRequestBehavior.AllowGet);
            return Json(true, JsonRequestBehavior.AllowGet);



Why don't you use the Remote validation for this?

Why posting back just to check if user exists?


public class RegisterModel
    [Remote("UserNameExists", "Validation", "", ErrorMessage = "Username is already taken.")]
    [RegularExpression(@"(\S)+", ErrorMessage = "White space is not allowed.")]
    [Display(Name = "Username")]
    public string UserName { get; set; }

and create a Validation Controller having the UserNameExists method like

public JsonResult UserNameExists(string UserName)
    var user = _db.Users.Where(x => x.username.Equals(UserName));

    return user == null ?
        Json(true, JsonRequestBehavior.AllowGet) :
        Json(string.Format("{0} is not available.", register.UserName), JsonRequestBehavior.AllowGet);

Need Your Help

Thread block for processing

c# .net wpf windows multithreading

I'm coding a C# wpf app and I need to block processing in order to wait for an event.

Android include tag not overriding layout attributes

android attributes include margin

I am having trouble using &lt;include&gt; in conjunction with &lt;merge&gt;. I am developing on Eclipse with the latest Android SDK (4.3). Here is my sample code: