簡體   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