1

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. http://blogs.msdn.com/b/simonince/archive/2011/02/04/conditional-validation-in-asp-net-mvc-3.aspx

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


Edit

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.

[Required]
    [DataType(DataType.EmailAddress)]
    [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);
        }

        if(userExists)
            return Json(UserManagementResources.UserAlreadyExists, JsonRequestBehavior.AllowGet);
        else
            return Json(true, JsonRequestBehavior.AllowGet);
    }

}
0

2 Answers 2

3

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

Why posting back just to check if user exists?

example:

public class RegisterModel
{
    [Required]
    [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);
}
Sign up to request clarification or add additional context in comments.

3 Comments

alright, I've added RemoteValidation, now it calls the validation and returns a value, but the validation message is still never displayed...
do you have the scripts in place? --> stackoverflow.com/questions/6016389/…
I had all the scripts, but the versions weren't the same in jquery.min.js and validation.min.js, it's working now, thanks a lot =)
1

When you change the version of your jQuery.js you have to change the validation.js file as well. Different versions are not compatible to each other and you might see strange behaviour in different browsers when you mixup the files.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.