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. 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);
    }

}

Answers


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);
}

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:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.