验证在 "mvc" 控制器中不起作用
Validations not working in "mvc" controller
我正在使用 Visual Studio 2013,并安装了 fluentvalidation
mvc,但验证不起作用,尽管程序运行良好
在执行程序时,fluentvalidation
函数不工作。我已完成所有步骤,但它不起作用
//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})?))$");
}
}
我正在使用 Visual Studio 2013,并安装了 fluentvalidation
mvc,但验证不起作用,尽管程序运行良好
在执行程序时,fluentvalidation
函数不工作。我已完成所有步骤,但它不起作用
//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})?))$");
}
}