![](/img/trans.png)
[英]How to get all role combinations, from a list, when roles can be combined to other roles
[英]How to check is Role in Roles list?
如果角色在角色列表集合中,我需要檢查一下,如果不是,則拋出驗證結果。 我該如何比較或以其他方式解決?
public class UserViewModel:IValidatableObject
{
[Required]
public string Username { get; set; }
public IEnumerable<string> Roles { get; set; }
[Required]
public string Rola { get; set; }
public UserViewModel()
{
Repozytorium db = new Repozytorium();
Roles = db.GetAllRoles();
}
public UserViewModel(string userName, string rola)
{
Repozytorium db = new Repozytorium();
Roles = db.GetAllRoles();
Username = userName;
Rola = rola;
}
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
if(Roles != Roles ) //this dont work
yield return new ValidationResult("role isnt valid", new string[] { "Rola" });
}
}
您正在檢查您的角色集合是否不是您的角色集合。 而是檢查Rola
是否不在集合中。 使用Linq:
if(Roles.All(x => x != Rola ))
yield return new ValidationResult("Role isn't valid", new [] {nameof(Rola)};
另外,我建議像示例中一樣使用nameof
,這樣,如果您更改屬性的名稱,錯誤消息將保持有效。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.