Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/437.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 流畅的验证不起作用_Asp.net Mvc_Asp.net Mvc 3_Fluentvalidation - Fatal编程技术网

Asp.net mvc 流畅的验证不起作用

Asp.net mvc 流畅的验证不起作用,asp.net-mvc,asp.net-mvc-3,fluentvalidation,Asp.net Mvc,Asp.net Mvc 3,Fluentvalidation,我调试了所有这些,并注意到,验证器构造函数被调用了,而不是一次,这很奇怪。这就是我的IoC工厂正常工作的原因。 使用服务调用的自定义验证规则和规则集正常工作,我调试了这些规则-进行了调用。但标准验证规则NotEmpty、Length、Matches和Must for Categories属性不起作用-ModelState对象中没有验证错误 这一切都很早就开始了,我不会更改这里发布的任何代码。未更改/添加全局模型绑定。我不知道 非工作验证模型的代码: 我的帖子: [HttpPost

我调试了所有这些,并注意到,验证器构造函数被调用了,而不是一次,这很奇怪。这就是我的IoC工厂正常工作的原因。 使用服务调用的自定义验证规则和规则集正常工作,我调试了这些规则-进行了调用。但标准验证规则NotEmpty、Length、Matches和Must for Categories属性不起作用-ModelState对象中没有验证错误

这一切都很早就开始了,我不会更改这里发布的任何代码。未更改/添加全局模型绑定。我不知道

非工作验证模型的代码:

我的帖子:

        [HttpPost]
        public ActionResult CreateTest([CustomizeValidator(RuleSet = "New")] Test model)
        {
            if (ModelState.IsValid)
            {
                var testId = testService.CreateTest(model);
                return RedirectToAction("Test", new { testId });
            }

            PrepareTestEdit(true);
            return View("EditTest");
        }
我的模型:

[Validator(typeof(TestValidator))]
public class Test
{
    public Test()
    {
        Categories = new List<string>();
    }

    public int Id { get; set; }

    public string Title { get; set; }

    public string Description { get; set; }

    public string UrlName { get; set; }

    public List<string> Categories { get; set; }
}
验证器:

public class TestValidator : AbstractValidator<Test>
{
    public TestValidator(ITestService testService)
    {
        RuleSet("Edit", () =>
            {
                RuleFor(x => x.Title).
                    Must((model, title) => testService.ValidateTitle(title, model.Id)).WithMessage("1");
                RuleFor(x => x.UrlName).
                    Must((model, urlName) => testService.ValidateUrlName(urlName, model.Id)).WithMessage("2");
            });

        RuleSet("New", () =>
        {
            RuleFor(x => x.Title).
                Must(title => testService.ValidateTitle(title)).WithMessage("3");
            RuleFor(x => x.UrlName).
                Must(urlName => testService.ValidateUrlName(urlName)).WithMessage("4");
        });

        RuleFor(x => x.Title).
            NotEmpty().WithMessage("5").
            Length(1, 100).WithMessage("6");
        RuleFor(x => x.UrlName).
            NotEmpty().WithMessage("7").
            Length(1, 100).WithMessage("8").
            Matches("^[-_a-zA-Z0-9]*$").WithMessage("9");
        RuleFor(x => x.Description).
            NotEmpty().WithMessage("10");
        RuleFor(x => x.Categories).
            Must(categories => categories != null && categories.Any()).WithMessage("11");
    }
}
规则集允许您将验证规则分组,这些规则可以作为一个组一起执行,同时忽略其他规则

我将所有常用规则放置在单独的私有方法中,并在匿名函数体中为这两个规则集调用它