我正在使用Visual Studio 2013,并安装了fluentvalidation
mvc,但验证不起作用,虽然该程序运行正常
执行程序时,fluentvalidation
功能无效。我已经完成了所有步骤,但它无法正常工作
答案 0 :(得分:1)
//Method in controller
public async Task<IHttpActionResult> Save(BacklogModifyVM item)
{
//Validate VM
var validator = new BacklogModifyVMValidator();
var result = validator.Validate(item);
if (!result.IsValid)
{
return InvalidDataResponse(result.Errors);
}
}
//entity class
public class BacklogModifyVM
{
public decimal BacklogId { get; set; }
public string BacklogTitle { get; set; }
public decimal BackLogStatusId { get; set; }
public string Owners { get; set; }
public decimal ProjectId { get; set; }
public string Description { get; set; }
public decimal? EpicId { get; set; }
}
//validator class
public class BacklogModifyVMValidator : AbstractValidator<BacklogModifyVM>
{
public BacklogModifyVMValidator()
{
RuleFor(x => x.BacklogId).GreaterThanOrEqualTo(-1).NotEqual(0);
RuleFor(x => x.BacklogTitle).NotEmpty();
RuleFor(x => x.BackLogStatusId).GreaterThan(0);
RuleFor(x => x.ProjectId).GreaterThan(0);
RuleFor(x => x.Owners).Matches(@"((\d+)((\.\d{1,2})?))$");
}
}