Home > Net >  Is there a way I can limit the number of group members of a group using blazor?
Is there a way I can limit the number of group members of a group using blazor?

Time:08-28

I have two related tables which represent a one to many relationship; that is the group table and the group member table. One group can have only up to five group members. How would I limit this number to only five so that never more than five members can be added to one group? The following are my C# classes.

public class Group
    {
        [Key]
        public int GroupId { get; set; }
        public string GroupName { get; set; }
        public int GroupNumber { get; set; }
        public string Product_Service { get; set; }
        public string BusinessLocation { get; set; }
        public List<GroupMember> GroupMembers { get; set; }
        public int CategoryId { get; set; }
        public Category Category { get; set; }   
        public List<Loan> Loans { get; set; }


    }
public class GroupMember
    {
        [Key]
        public int MemberId { get; set; }
        public int MemberNumber { get; set; }
        [Required, RegularExpression(@"^\d{6}\/\d{2}\/\d{1}$", ErrorMessage = "Please enter a valid NRC. xxxxxx/xx/x")]
        public string Nrc { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Address { get; set; }
        public string PhoneNumber { get; set; }
        public int GroupId { get; set; }
        public Group Group { get; set; }

CodePudding user response:

I Think that you want to add your own validation:

public class CountGroupMembersAttribute : ValidationAttribute
{
 private readonly int count;
 
 public CountGroupMembersAttribute(int count)
 {
   this.count =  count;
 }
 
 protected overrride ValidationResult IsValid(object value, ValidationContext context)
 {
   List<GroupMember> groups= value as List<GroupMember>;
  
   if(groups == null)
    return validation.Success; 
  
  if(groups.Count > 5)
  {
  return new ValidationResult("the following property has been exceeded the limit", new[] { context.MemberName });
  }
 }
}

Then use it like this:

[CountGroupMembers(count:5)]
public List<GroupMember> GroupMembers { get; set; }
  • Related