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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T19:56:27+00:00 2026-06-01T19:56:27+00:00

I would like to validate that a username is not already in use when

  • 0

I would like to validate that a username is not already in use when adding or editing a user in my solution.

Here is what I do with the remote validation:

ViewModel:

    [Required, Remote("UserNameAlreadyExists", "User", Error="Already exists!")]
    public string UserName { get; set; }

Controller:

    public ActionResult UserNameAlreadyExists(string userName)
    {
        var user = _requestServiceClient.GetUserFromUserName(userName);
        return Json(user == null, JsonRequestBehavior.AllowGet);
    }

It works pretty well for the creation. But It doesn’t work with edition because the username already exist for the edited user himself. Do you know what I mean?

A solution would be to check in DB based on UserName and UserID. So in the case of editing a user, we can check that the username does not exist yet (except the userid passed as parameter). Is it a good alternative? How to pass this userID in the Remote validation attribute?

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-01T19:56:29+00:00Added an answer on June 1, 2026 at 7:56 pm

    If your users cannot change their username after creation then use a different view model for the editing where you just display the UserName without any validation.

    Otherwise you can use the AdditinalFields property to pass extra data to your validator:

    public class UserModel
    {
        public int Id{ get; set; }
    
        [Required, Remote("UserNameAlreadyExists", "User", AdditionalFields = "Id")]
        public string UserName { get; set; }
    }
    
    public ActionResult UserNameAlreadyExists(string userName, int id)
    {
        var user = _requestServiceClient.GetUserFromUserNameAndId(userName, id);
        return Json(user == null, JsonRequestBehavior.AllowGet);
    }
    

    You will also need to include the Id property in your form eg:

    @using (Html.BeginForm())
    {
        @Html.HiddenFor(m => m.Id)
        @Html.TextBoxFor(m => m.UserName)
        @Html.ValidationMessageFor(x => x.UserName)
        <input type="submit" value="Save" />
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to validate a form so that it checks if the user
I would like to have the regex to validate that user has entered any
I would like to validate IP addresses from a list that may contain incorrectly
I have a validation class which I would like to use to check all
I would really like use the jQuery Validation plugin in my ASP.NET Web Forms
I would like to validate that an element 'Test' should Have its content restricted
I have a form that I would like to validate that a field exist
I have an input field which I would like to dynamically validate while user
I would like to validate an email using the RegexValidator, e.g. [RegexValidator(@^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$)] Which works
I would like to validate the DNF (Disjunctive normal form) which looks like this

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.