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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:04:38+00:00 2026-05-23T08:04:38+00:00

Long-time reader, first time poster. I’m using ASP MVC 2 and remote validation. I

  • 0

Long-time reader, first time poster.

I’m using ASP MVC 2 and remote validation. I have a model called PersonVM, which AddPersonVM and EditPersonVM inherit from. This is so I can use one usercontrol to handle most of the UI that is the same between both subclassed models.

/// <summary>
/// This class handles adding and editing people.
/// </summary>
[MustHaveAtLeastOneOf("HomePhoneNumber", "CellPhoneNumber", ErrorMessage="At least one of the phone numbers must be filled in.")]
public abstract class PersonVM : WebViewModel
{

    #region Reference (for Binding)

    /// <summary>
    /// Provides a list of person types, e.g. Teacher, Student, Parent.
    /// </summary>
    public IEnumerable<PersonType> ListOfPersonTypes {
        get {
            if (_listOfPersonTypes == null) {
                _listOfPersonTypes = Data.PersonTypes
                    .Where(pt => pt.OrganizationID == ThisOrganization.ID || pt.OrganizationID == null)
                    .OrderBy(pt => pt.Name).ToArray();
            }
            return _listOfPersonTypes;
        }
    }
    private IEnumerable<PersonType> _listOfPersonTypes = null;

    /// <summary>
    /// Provides a list of schools the person can belong to.
    /// </summary>
    public IEnumerable<School> ListOfSchools {
        get {
            if (_listOfSchools == null) {
                _listOfSchools = Data.Schools
                    .Where(s =>
                        (s.OrganizationID == ThisOrganization.ID || s.OrganizationID == null) // TODO
                        && s.Deleted == false
                        )
                    .OrderBy(s => s.Name);
            }
            return _listOfSchools;
        }
    }
    private IEnumerable<School> _listOfSchools = null;

    /// <summary>
    /// Provides a list of contact types, e.g. Home phone, email.
    /// </summary>
    public IEnumerable<ContactType> ListOfContactTypes {
        get {
            if (_listOfContactTypes == null) {
                _listOfContactTypes = Data.ContactTypes
                    .OrderBy(ct => ct.Name);
            }
            return _listOfContactTypes;
        }
    }
    private IEnumerable<ContactType> _listOfContactTypes = null;

    /// <summary>
    /// Provides a list of genders.
    /// </summary>
    public IEnumerable<string> Genders {
        get {
            return new string[] { null, "Male", "Female" };
        }
    }

    /// <summary>
    /// Returns a list of US states.
    /// </summary>
    public List<StaticData.USState> ListOfUSStates {
        get {
            return StaticData.USStates;
        }
    }

    #endregion

    /// <summary>
    /// Represents the current person being edited.
    /// </summary>
    public Person CurrentPerson { get; set; }

    #region Abstracted Required Fields
    /*
     * This is done, since address information, DOB
     * are not required on the panel, but are required fields here. 
     * I tried implementing an interface called IPersonAddressRequired 
     * with this information in it, but it didn't work. Additionally,
     * only one instance of [MetadataType] is allowed on a class,
     * so that didn't work either.
     */

    [Required]
    [StringLength(100)]
    public string Address {
        get { return CurrentPerson.Address; }
        set { CurrentPerson.Address = value; }
    }

    [StringLength(100)]
    public string Address2 {
        get { return CurrentPerson.Address2; }
        set { CurrentPerson.Address2 = value; }
    }

    [Required]
    [StringLength(50)]
    public string City {
        get { return CurrentPerson.City; }
        set { CurrentPerson.City = value; }
    }

    [Required]
    [StringLength(50)]
    public string State {
        get { return CurrentPerson.State; }
        set { CurrentPerson.State = value; }
    }

    [Required]
    [StringLength(50)]
    public string Zip {
        get { return CurrentPerson.Zip; }
        set { CurrentPerson.Zip = value; }
    }

    [DataType(DataType.Date)]
    [Required]
    public DateTime? DOB {
        get { return CurrentPerson.DOB; }
        set { CurrentPerson.DOB = value; }
    }

    [Required]
    public string Gender {
        get { return CurrentPerson.Gender; }
        set { CurrentPerson.Gender = value; }
    }

    #endregion

    #region Abstracted Contact Methods

    /// <summary>
    /// When adding someone, this represents the phone number contact record.
    /// </summary>
    [Display(Name = "Home Phone Number")]
    [DisplayName("Home Phone Number")]
    [USPhoneNumber]
    //[Required]
    public string HomePhoneNumber { get; set; }

    /// <summary>
    /// When adding someone, this represents the phone number contact record.
    /// </summary>
    [Display(Name = "Cell Phone Number")]
    [DisplayName("Cell Phone Number")]
    [USPhoneNumber]
    //[Required]
    public string CellPhoneNumber { get; set; }

    /// <summary>
    /// When adding someone, this represents the email address contact record.
    /// </summary>
    [Display(Name = "Email Address")]
    [DisplayName("Email Address")]
    [Required]
    [UniqueEmailAddress(ErrorMessage="The email address was already found in our system, please sign in or use another email.")]
    [Email(ErrorMessage="The email address specified isn't an email address.")]
    public string EmailAddress { get; set; }

    #endregion

    //////////////////////////////

    /// <summary>
    /// Sets the picture to be uploaded.
    /// </summary>
    public byte[] PictureData { get; set; }

    /// <summary>
    /// One of 'keep', 'remove', 'replace'.
    /// </summary>
    public string PictureAction { get; set; }

}

I have 2 models that inherit from this model, AddPersonVM and EditPersonVM. Each implements how they handle submitting changes a little differently.

[UniqueEmailAddress] is my RemoteAttribute that checks for an existing email address, and what I have issue with.

This works fine on adding someone, but when editing an existing person, the email address already stored exists in the membership system, so it fails validation. What I would like to do is store the original value of the property’s value, and exclude that from what IsValid checks.

My code:

/// <summary>
/// Checks to be sure that an entered email address is unique.
/// </summary>
public class UniqueEmailAddressAttribute : RemoteAttribute
{

    /// <summary>
    /// Returns true if the email address specified is currently in use.
    /// </summary>
    /// <param name="emailAddress"></param>
    /// <returns></returns>
    public static bool IsEmailAddressInUse(string emailAddress) {
        return (Membership.FindUsersByEmail(emailAddress).Count != 0);
    }

    ///////////////////////////

    public UniqueEmailAddressAttribute() : base("UniqueEmailAddress", "RemoteValidation", "emailAddress") { }

    ///////////////////////////

    public override bool IsValid(object value) {

        // If value is null, return true, since a [Required] attribute handles required values.
        if (value == null)
            return true;

        // Value must be a string.
        if (!(value is string))
            return false; 

        // We're not checking the validity of the email address here,
        // the [EmailAddress] attribute handles that for us.

        // Check the email's uniqueness.
        return !IsEmailAddressInUse((string)value);

    }

}

What I would like to have:


///
/// Checks to be sure that an entered email address is unique.
///
public class UniqueEmailAddressAttribute : RemoteAttribute
{

string OriginalValue = null;

    /// <summary>
    /// Returns true if the email address specified is currently in use.
    /// </summary>
    /// <param name="emailAddress"></param>
    /// <returns></returns>
    public static bool IsEmailAddressInUse(string emailAddress) {
        return (Membership.FindUsersByEmail(emailAddress).Count != 0);
    }

    ///////////////////////////

    public UniqueEmailAddressAttribute() : base("UniqueEmailAddress", "RemoteValidation", "emailAddress") { 
OriginalValue = value Found using reflection or something;
}

    ///////////////////////////

    public override bool IsValid(object value) {

        // If value is null, return true, since a [Required] attribute handles required values.
        if (value == null)
            return true;

        // Value must be a string.
        if (!(value is string))
            return false; 

        // We're not checking the validity of the email address here,
        // the [EmailAddress] attribute handles that for us.

if ((string)value == OriginalValue)
return true;

        // Check the email's uniqueness.
        return !IsEmailAddressInUse((string)value);

    }

}


Any ideas? As of now, I could add the EmailAddress property individually to both AddPersonVM and EditPersonVM, take the field out of my usercontrol view and add to both views, and remove the attribute on Edit, but then that would stop noone from editing their account to have whatever email address they want. I don’t care if reflection is required to read the original value, that’s cool.

Thanks in advance! – Derreck

  • 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-23T08:04:38+00:00Added an answer on May 23, 2026 at 8:04 am

    I think you want to add person with unique email address and if any user try to change his/her email address which is in use you want to check uniquness if he change, but you also don’t want to check uniquness if he does not change his email address.

    So my suggestion is add one more property to IsEmailAddressInUse (string email, bool IsEdit).
    If IsEdit is true then check email address from user loggedin info. If same return true if not check uniquness.

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

Sidebar

Related Questions

Long time reader, first time poster. Any help is greatly appreciated. I have crafted
Long term lurker, first time poster here. I have written a class to model
long time reader/first time poster here. So I've got a checkbox array that posted
Long time reader, first time poster asks: After many weeks of google and forum
Long time reader, first time poster. I'm a big noob when it comes to
long time reader, first time poster. I’m coming with an issue that many of
Long time reader, first time poster. Working on a web site at http://www.howardpitch.com/ ,
first time poster, long time reader so be gentle with me :) See the
(Long time reader of SO, first time asking a q. I'm quite new to
Long time listener, first time caller. I'm a full time SE during the day

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.