Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5840369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T11:44:04+00:00 2026-05-22T11:44:04+00:00

I have an MVC 3 application and am trying to display a custom validation

  • 0

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

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T11:44:06+00:00Added an answer on May 22, 2026 at 11:44 am

    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);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an asp.net mvc application and i am trying to assign value to
I have an MVC application that among other things contains a small Silverlight menu
I have an application, built using MVC, that produces a view which delivers summary
I have an ASP.NET MVC application and I'm trying to load the following html
I currently have an ASP.NET MVC application that exists. I want to add a
I have an .Net MVC application which runs fine if I use the build
I have an ASP.NET MVC-application which I want deployable on both IIS6 and IIS7
I have an asp.net mvc application with a route similar to: routes.MapRoute(Blog, {controller}/{action}/{year}/{month}/{day}/{friendlyName}, new
I have an ASP.NET MVC application, when a user clicks on the submit button
I have a very nice MVC Beta application developed using VS2008 on a win2008

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.