gpt4 book ai didi

asp.net-mvc - 流利验证可确保列表中至少包含一项属性值为somevalue的项目

转载 作者:行者123 更新时间:2023-12-04 17:47:03 25 4
gpt4 key购买 nike

假设我有以下 View 模型:

public class TaskViewModel{
public MTask Task {get;set;}
public List<DocIdentifier> Documents {get;set;}
.....
}

public class DocIdentifier{
public string DocID {get;set;}
public bool Selected {get;set;}
}

这是我使用的 Fluent Validation验证器:
public class TaskValidator : AbstractValidator<TaskViewModel>{
public TaskValidator{

}
}

如何确定列表 Documents中的至少一个DocIdentifier对象具有其 Selected属性值 True

最佳答案

您必须使用谓词验证程序Must,在其中您可以基于LINQ扩展名指定自定义条件:

public class TaskValidator : AbstractValidator<TaskViewModel>{
public TaskValidator()
{
RuleFor(task => task.Documents)
.Must(coll => coll.Any(item => item.Selected)) // you can secify custom condition in predicate validator
.WithMessagee("At least one of {0} documents should be selected",
(model, coll) => coll.Count); // error message can use validated collection as well as source model
}
}

关于asp.net-mvc - 流利验证可确保列表中至少包含一项属性值为somevalue的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30521739/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com