繁体   English   中英

有没有办法可以限制使用 blazor 的组的组成员数量?

[英]Is there a way I can limit the number of group members of a group using blazor?

我有两个相关的表,它们代表一对多的关系; 即组表和组成员表。 一个组最多只能有五个组成员。 我如何将这个数字限制为只有五个,以便一个组中添加的成员不超过五个? 以下是我的 C# 课程。

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; }

我认为您想添加自己的验证:

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 });
  }
 }
}

然后像这样使用它:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM