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 9272825
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:57:17+00:00 2026-06-18T15:57:17+00:00

With this code I check whether a username is valid or not: public class

  • 0

With this code I check whether a username is valid or not:

public class UniqueUsernameAttribute : ValidationAttribute
{
    public override bool IsValid(object value)
    {
        IRepository<User> userRepository = new EFRepository<User>();

        User user = userRepository.GetAll().FirstOrDefault(x => x.Name.Equals((string) value));

        return user == null;
    }
}

This works fine when adding new users. But when editing users, one existing one will already be present in the database and so this code doesn’t work anymore (as I check if there are 0 entries, while 1 will exist already). Is there any way to add an extra parameter or something?

Thanks

  • 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-06-18T15:57:18+00:00Added an answer on June 18, 2026 at 3:57 pm

    You might try what this CodeProject article suggests: http://www.codeproject.com/Articles/260177/Custom-Validation-Attribute-in-ASP-NET-MVC3

    Overriding the other IsValid would give you the options to include additional data including whether you are really looking for a new – non-dupliated user, or whether you are looking at an existing user.

    using System.ComponentModel.DataAnnotations;
    
    public class testattribute : ValidationAttribute    
    {
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            return base.IsValid(value, validationContext);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my code to check whether class has member function begin or not
i have this code in c# doc.SelectSingleNode(WhoisRecord/registrant/email).InnerText how can i check whether it is
Hi i am using the following code to check whether the username and password
I am using this code to check whether a value (guid1) already exists in
I wrote this sample code to check whether integer or string index is better
Possible Duplicate: Check whether $_POST-value is empty I am using this code to validate
I'm trying to check whether a user has liked my app using this code:
I am writing code to check whether a file exists or not. If the
I am using this code to check if the request came from a page
I am using this code to check that array is present in the HashMap

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.