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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:36:27+00:00 2026-05-13T12:36:27+00:00

I’m having problems with getting my custom dataannotations to work, I’m trying to add

  • 0

I’m having problems with getting my custom dataannotations to work, I’m trying to add a validation-attribute that validates that the UsergroupName for a Customer (CustomerID) is unique.

[MetadataType(typeof(UsergroupMetaData))]
public partial class Usergroup { }

public class UsergroupMetaData
{
    [Required()]
    public object CustomerID { get; set; }

    [Required(ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = "UsergroupNameRequired")]
    public object UsergroupName { get; set; }

    [UniqueUsergroupName(????)]
    // what to put here?
}



public class UniqueUsergroupName : ValidationAttribute
{
    UsergroupRepository _rep = new UsergroupRepository();

    public override bool IsValid(object value, int customerID)
    {
        var x = _rep.GetUsergroups().ByUsergroupName(value).ByCustomerID(customerID);

        // what to put here?

        return false;
    }
}

the IsValid should return false if the “count >0”.

How do I fix this one so it works. GetUsergroups() returns IQueryable.

EDIT:

[MetadataType(typeof(UsergroupMetaData))]
public partial class Usergroup { }

public class UsergroupMetaData
{
    public object CustomerID { get; set; }

    [Required(ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = "UsergroupNameRequired")]
    [UniqueUsergroupName(ErrorMessageResourceType= typeof(Resources), ErrorMessageResourceName = "UsergroupNameExists")]
    public object UsergroupName { get; set; }


}


public class UniqueUsergroupName : ValidationAttribute
{

    UsergroupRepository _rep = new UsergroupRepository();

    public override bool IsValid(object value, int customerID)
    {

        int usergroups = _rep.GetUsergroups().ByCustomerID(customerID).ByUsergroupName(value.ToString()).Count();

        return usergroups >0;
    }
}

How can I pass the current CustomerID as a parameter?

/M

  • 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-13T12:36:27+00:00Added an answer on May 13, 2026 at 12:36 pm

    You can apply this approach
    http://byatool.com/mvc/custom-data-annotations-with-mvc-how-to-check-multiple-properties-at-one-time/
    To include the ID property in your search, so that you check for all “other” UsergroupMetaData
    that have the same UsergroupName.

    Check it and tel me if you have trouble applying it to your scenario.

    Edit: More explanation

    My understanding is that you need to check whether there are other UsergroupMetaData objects with the same UsergroupName.

    Let’s assume we’ll make the validator become on your entire class not the property:

    [UniqueUsergroupName]
    public class UsergroupMetaData
    

    No parameters are needed. Let’s see how the UniqueUsergroupName Validate() method will look like:

    public override Boolean IsValid(Object value)
    {
      var usergroupName = value != null ? value.ToString() : null;
      //We don't validate empty fields, the Required validator does that
      if(string.IsNullOrEmpty(usergroupName)) return true;
    
      Type objectType = value.GetType();
      //Get the property info for the object passed in.  This is the class the attribute is
      //  attached to
      //I would suggest caching this part... at least the PropertyInfo[]
      PropertyInfo[] neededProperties =
        objectType.GetProperties();
    
      var customerIdProperty = neededProperties
        .Where(propertyInfo => propertyInfo.Name == "CustomerID")
        .First();
      var customerId = (int?) customerIdProperty.GetValue(value, null);
    
      var usergroupNameProperty = neededProperties
        .Where(propertyInfo => propertyInfo.Name == "UsergroupName")
        .First();
      var usergroupName = (string) customerIdProperty.GetValue(value, null);
    
      // Now I don't userstand why the blog post author did all this reflection stuff to 
      //   get the values of the properties. I don't know why he just didn't d something like:
      // var usergroup = (Usergroup) value;
      // var customerId = usergroup.CustomerId;
      // var usergroupName = usergroup.UsergroupName;
      //
      //I think reflection was not needed here. Try both ways anyway.
      // The next lines should not be different regardless of whether you used reflection.
      // 
    
      //We don't validate empty fields, the Required validator does that
      if(string.IsNullOrEmpty(usergroupName)) return true;
    
      //Now you have the customerId and usergroupName. Use them to validate.
      //If I'm using LINQ (for explanation only) it'd be something like:
      // Assuming _rep.GetUsergroups() returns IQueryable (only for explanation):
      int numberOfOtherUsergroupsWithSameName = 
            _rep.GetUsergroups()
                  .Where(g => g.UsergroupName == usergroupName && g.CustomerId != customerId)
                  .Count();
      return numberOfOtherUsergroupsWithSameName == 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 373k
  • Answers 373k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Yes you need to access the QueryString collection to get… May 14, 2026 at 7:38 pm
  • Editorial Team
    Editorial Team added an answer Use the font size style? http://livedocs.adobe.com/flex/3/langref/mx/controls/Label.html#style:fontSize May 14, 2026 at 7:38 pm
  • Editorial Team
    Editorial Team added an answer No it's not but you can just hide the banners… May 14, 2026 at 7:38 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.