【发布时间】:2014-01-23 13:36:56
【问题描述】:
我正在一个包含复杂视图模型的项目上尝试 FluentValidation,我阅读了 documentation here,但我不知道如何设置规则来验证在我的视图模型中声明的对象列表。在下面的示例中,视图模型中的列表包含 1 个或多个 Guitar 对象。谢谢
查看模型
[FluentValidation.Attributes.Validator(typeof(CustomerViewModelValidator))]
public class CustomerViewModel
{
[Display(Name = "First Name")]
public string FirstName { get; set; }
[Display(Name = "Last Name")]
public string LastName { get; set; }
[Display(Name = "Phone")]
public string Phone { get; set; }
[Display(Name = "Email")]
public string EmailAddress { get; set; }
public List<Guitar> Guitars { get; set; }
}
View Model 中使用的吉他类
public class Guitar
{
public string Make { get; set; }
public string Model { get; set; }
public int? ProductionYear { get; set; }
}
查看模型验证器类
public class CustomerViewModelValidator : AbstractValidator<CustomerViewModel>
{
public CustomerViewModelValidator()
{
RuleFor(x => x.FirstName).NotNull();
RuleFor(x => x.LastName).NotNull();
RuleFor(x => x.Phone).NotNull();
RuleFor(x => x.EmailAddress).NotNull();
//Expects an indexed list of Guitars here????
}
}
【问题讨论】:
-
你不能有单独的吉他类验证器吗?你想在 Ilist 上进行什么样的验证,是否有多个元素?
标签: c# asp.net-mvc-3 fluentvalidation