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

  • SEARCH
  • Home
  • 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 907299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:32:01+00:00 2026-05-15T16:32:01+00:00

Can the built in ASP MVC validation be made to behave differently for different

  • 0

Can the built in ASP MVC validation be made to behave differently for different actions of a same controller ? For example I have a user controller and it has actions like create, edit and other actions. So in model user the attribute Username is being validated for its uniqueness. If there is an user present with the same username, it throws and error username already present. So using the same validator for edit action throws an error “username already present” while editing an user. Can anybody tell me if there is a way to do solve this problem? I am pasting my validator code for reference.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.ComponentModel.DataAnnotations;  

    namespace Models
    {
        [MetadataType(typeof(AdmiUserMetadata))]
        public partial class AdminUser
        {
            public class AdmiUserMetadata
            {
                [Required(ErrorMessage = "Required Field")]
                public string Id { get; set; }

                [Required(ErrorMessage = "Required Field")]
                [RegularExpression("[\\S]{6,}", ErrorMessage = "Must be at least 6 characters.")]
                [Username(ErrorMessage = "Username already taken")]
                public string Username { get; set; }

                [Required(ErrorMessage = "Required Field")]
                [RegularExpression("[\\S]{6,}", ErrorMessage = "Must be at least 6 characters.")]
                public string Password { get; set; }

                [Required(ErrorMessage = "Required Field")]
                public string Name { get; set; }

                [Required(ErrorMessage = "Required Field")]
                [RegularExpression("^[a-z0-9_\\+-]+(\\.[a-z0-9_\\+-]+)*@[a-z0-9-]+(\\.[a-z0-9-]+)*\\.([a-z]{2,4})$", ErrorMessage ="Invalid E-mail ID")]
                public string Email { get; set; }

                [Required(ErrorMessage = "Required Field")]
                [RegularExpression("(Active|Disabled)", ErrorMessage = "Select the status of User")]
                public string Status { get; set; }

                [Required(ErrorMessage = "Required Field")]
                [RegularExpression("^[1-9]", ErrorMessage = "Select the group of User")]
                public string Group { get; set; }
            }
        }

        public class UsernameAttribute : ValidationAttribute
        {
            IUserRepository _repository = new UserRepository();
            public override bool IsValid(object value)
            {
                if (value == null)  
                    return true;
                if (_repository.IsUsernamePresent((string)value))
                {
                    return false;
                }
                return true;
            }
        }
    }
  • 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-15T16:32:02+00:00Added an answer on May 15, 2026 at 4:32 pm

    What you are validating is a business rule.

    No two users can have the same username.
    

    I would have a User service that enforces this rule on creation/edit. Attributes are best suited for input validation. (eg Is the integer non-negative? A valid email address? etc)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have built an ASP.NET mvc web application and it seems that some of
Hi, I have built a ASP.NET MVC website that uses Membership, this works fine.
I have an existing complex website built using ASP.NET MVC, including a database backend,
In asp.net mvc I always see the built in html helpers they always have
I have built an ASP.NET MVC web application that uses Entity Framework. To provide
I have built an ASP.NET MVC 3 web application (with exlusively Razor/cshtml pages) that
DUPE: Any Yahoo YUI validation framework that I can implement in asp.net MVC? Has
just wondering if there is something built into asp.net mvc where I can override
I have a custom CMS built with ASP.NET WebForms (you can see it in
Can you run the same web application (ASP.NET MVC 2) on ASP.NET 2 as

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.