C# .NET MVC FluentValidation无法处理列表

C# .NET MVC FluentValidation无法处理列表,c#,asp.net-mvc-3,fluentvalidation,C#,Asp.net Mvc 3,Fluentvalidation,全部, 这是我第一个使用FluentValidation的项目——我确实读过,但出于某种原因,我的对象列表并没有被悄悄地验证。其他非列表属性正在悄悄地进行验证,并且ModelState在控制器中的form POST中被报告为无效 我还可以看到列表属性的呈现HTML不包含标准验证属性 呈现为这样: <input type="text" value="" name="Guitars[0].Make" id="Guitars_0__Make" placeholder="Make"> 视图

全部,

这是我第一个使用FluentValidation的项目——我确实读过,但出于某种原因,我的对象列表并没有被悄悄地验证。其他非列表属性正在悄悄地进行验证,并且ModelState在控制器中的form POST中被报告为无效

我还可以看到列表属性的呈现HTML不包含标准验证属性

呈现为这样:

<input type="text" value="" name="Guitars[0].Make" id="Guitars_0__Make" placeholder="Make">
视图模型

 [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; } 
    }
}

列表验证程序

 public class GuitarValidator : AbstractValidator<Guitar>
    {
        public GuitarValidator()
        {
            // All your other validation rules for Guitar. eg.
            RuleFor(x => x.Make).NotEmpty();
            RuleFor(x => x.Model).NotEmpty();
            RuleFor(x => x.ProductionYear).NotEmpty();
        }
    }
公共类GuitarValidator:AbstractValidator
{
公共GuitarValidator()
{
//所有其他吉他验证规则。
RuleFor(x=>x.Make).NotEmpty();
RuleFor(x=>x.Model).NotEmpty();
(x=>x.ProductionYear).NotEmpty()的规则;
}
}
视图

    <script src="/Scripts/jquery-1.10.2.min.js" type="text/javascript"></script>
    <script src="/Scripts/jquery.validate.min.js" type="text/javascript"></script>
    <script src="/Scripts/jquery.validate.unobtrusive.min.js" type="text/javascript"></script>
    <script src="/Scripts/jquery.unobtrusive-ajax.js" type="text/javascript"></script>


     <!-- List Object #1 -->
     <div id="cosponsorsTemplate_1">
            <div class="formColumn1"><label>Guitar</label></div>
            <div class="formColumn2">@Html.TextBoxFor(model => model.Guitars[0].Make, new { Placeholder = "Make" })
                <div class="messageBottom">@Html.ValidationMessageFor(model => model.Guitars[0].Make)</div>
            </div>
            <div class="formColumn3">@Html.TextBoxFor(model => model.Guitars[0].Model, new { Placeholder = "Model" })
                <div class="messageBottom">@Html.ValidationMessageFor(model => model.Guitars[0].Model)</div>
            </div>
            <div class="formColumn4">@Html.TextBoxFor(model => model.Guitars[0].ProductionYear, new { Placeholder = "Production Year" })
                <div class="messageBottom">@Html.ValidationMessageFor(model => model.Guitars[0].ProductionYear)</div>
                <a class="icon delete">Delete</a>
            </div>
        </div>

   <!-- List Object #2 -->
        <div id="cosponsorsTemplate_2">
            <div class="formColumn1"><label>Guitar</label></div>
            <div class="formColumn2">@Html.TextBoxFor(model => model.Guitars[1].Make, new { Placeholder = "Make" })
                <div class="messageBottom">@Html.ValidationMessageFor(model => model.Guitars[1].Make)</div>
            </div>
            <div class="formColumn3">@Html.TextBoxFor(model => model.Guitars[1].Model, new { Placeholder = "Model" })
                <div class="messageBottom">@Html.ValidationMessageFor(model => model.Guitars[1].Model)</div>
            </div>
            <div class="formColumn4">@Html.TextBoxFor(model => model.Guitars[1].ProductionYear, new { Placeholder = "Production Year" })
                <div class="messageBottom">@Html.ValidationMessageFor(model => model.Guitars[1].ProductionYear)</div>
                <a class="icon delete">Delete</a>
            </div>
        </div>

    <!-- List Object #3 -->
        <div id="cosponsorsTemplate_3">
            <div class="formColumn1"><label>Guitar</label></div>
            <div class="formColumn2">@Html.TextBoxFor(model => model.Guitars[2].Make, new { Placeholder = "Make" })
                <div class="messageBottom">@Html.ValidationMessageFor(model => model.Guitars[2].Make)</div>
            </div>
            <div class="formColumn3">@Html.TextBoxFor(model => model.Guitars[2].Model, new { Placeholder = "Model" })
                <div class="messageBottom">@Html.ValidationMessageFor(model => model.Guitars[2].Model)</div>
            </div>
            <div class="formColumn4">@Html.TextBoxFor(model => model.Guitars[2].ProductionYear, new { Placeholder = "Production Year" })
                <div class="messageBottom">@Html.ValidationMessageFor(model => model.Guitars[2].ProductionYear)</div>
                <a class="icon delete">Delete</a>
            </div>
        </div>

吉他
@Html.TextBoxFor(model=>model.Guitars[0].Make,新建{Placeholder=“Make”})
@Html.ValidationMessageFor(model=>model.Guitars[0].Make)
@Html.TextBoxFor(model=>model.Guitars[0]。model,新建{Placeholder=“model”})
@Html.ValidationMessageFor(model=>model.Guitars[0].model)
@Html.TextBoxFor(model=>model.Guitars[0].ProductionYear,新{Placeholder=“Production Year”})
@Html.ValidationMessageFor(model=>model.Guitars[0].ProductionYear)
删去
吉他
@Html.TextBoxFor(model=>model.Guitars[1].Make,new{Placeholder=“Make”})
@Html.ValidationMessageFor(model=>model.Guitars[1].Make)
@Html.TextBoxFor(model=>model.Guitars[1]。model,新建{Placeholder=“model”})
@Html.ValidationMessageFor(model=>model.Guitars[1].model)
@Html.TextBoxFor(model=>model.Guitars[1].ProductionYear,新{Placeholder=“Production Year”})
@Html.ValidationMessageFor(model=>model.Guitars[1].ProductionYear)
删去
吉他
@Html.TextBoxFor(model=>model.Guitars[2].Make,new{Placeholder=“Make”})
@Html.ValidationMessageFor(model=>model.Guitars[2].Make)
@Html.TextBoxFor(model=>model.Guitars[2]。model,新建{Placeholder=“model”})
@Html.ValidationMessageFor(model=>model.Guitars[2].model)
@Html.TextBoxFor(model=>model.Guitars[2].ProductionYear,新{Placeholder=“Production Year”})
@Html.ValidationMessageFor(model=>model.Guitars[2].ProductionYear)
删去

这里明显的问题是,您是否在应用程序启动代码中添加了以下内容?FluentValidationModelValidatorProvider.Configure();您是否也确保JavaScript验证文件包含在您的_Layout.cshtml文件中?您是否看到了这两个问题:和?我没有看到这些问题,但似乎集合验证仅在服务器端可用:客户端不支持集合验证程序。Mvc对客户端规则的支持非常有限,只能在客户端上运行FluentValidation规则的一小部分。
 public class GuitarValidator : AbstractValidator<Guitar>
    {
        public GuitarValidator()
        {
            // All your other validation rules for Guitar. eg.
            RuleFor(x => x.Make).NotEmpty();
            RuleFor(x => x.Model).NotEmpty();
            RuleFor(x => x.ProductionYear).NotEmpty();
        }
    }
    <script src="/Scripts/jquery-1.10.2.min.js" type="text/javascript"></script>
    <script src="/Scripts/jquery.validate.min.js" type="text/javascript"></script>
    <script src="/Scripts/jquery.validate.unobtrusive.min.js" type="text/javascript"></script>
    <script src="/Scripts/jquery.unobtrusive-ajax.js" type="text/javascript"></script>


     <!-- List Object #1 -->
     <div id="cosponsorsTemplate_1">
            <div class="formColumn1"><label>Guitar</label></div>
            <div class="formColumn2">@Html.TextBoxFor(model => model.Guitars[0].Make, new { Placeholder = "Make" })
                <div class="messageBottom">@Html.ValidationMessageFor(model => model.Guitars[0].Make)</div>
            </div>
            <div class="formColumn3">@Html.TextBoxFor(model => model.Guitars[0].Model, new { Placeholder = "Model" })
                <div class="messageBottom">@Html.ValidationMessageFor(model => model.Guitars[0].Model)</div>
            </div>
            <div class="formColumn4">@Html.TextBoxFor(model => model.Guitars[0].ProductionYear, new { Placeholder = "Production Year" })
                <div class="messageBottom">@Html.ValidationMessageFor(model => model.Guitars[0].ProductionYear)</div>
                <a class="icon delete">Delete</a>
            </div>
        </div>

   <!-- List Object #2 -->
        <div id="cosponsorsTemplate_2">
            <div class="formColumn1"><label>Guitar</label></div>
            <div class="formColumn2">@Html.TextBoxFor(model => model.Guitars[1].Make, new { Placeholder = "Make" })
                <div class="messageBottom">@Html.ValidationMessageFor(model => model.Guitars[1].Make)</div>
            </div>
            <div class="formColumn3">@Html.TextBoxFor(model => model.Guitars[1].Model, new { Placeholder = "Model" })
                <div class="messageBottom">@Html.ValidationMessageFor(model => model.Guitars[1].Model)</div>
            </div>
            <div class="formColumn4">@Html.TextBoxFor(model => model.Guitars[1].ProductionYear, new { Placeholder = "Production Year" })
                <div class="messageBottom">@Html.ValidationMessageFor(model => model.Guitars[1].ProductionYear)</div>
                <a class="icon delete">Delete</a>
            </div>
        </div>

    <!-- List Object #3 -->
        <div id="cosponsorsTemplate_3">
            <div class="formColumn1"><label>Guitar</label></div>
            <div class="formColumn2">@Html.TextBoxFor(model => model.Guitars[2].Make, new { Placeholder = "Make" })
                <div class="messageBottom">@Html.ValidationMessageFor(model => model.Guitars[2].Make)</div>
            </div>
            <div class="formColumn3">@Html.TextBoxFor(model => model.Guitars[2].Model, new { Placeholder = "Model" })
                <div class="messageBottom">@Html.ValidationMessageFor(model => model.Guitars[2].Model)</div>
            </div>
            <div class="formColumn4">@Html.TextBoxFor(model => model.Guitars[2].ProductionYear, new { Placeholder = "Production Year" })
                <div class="messageBottom">@Html.ValidationMessageFor(model => model.Guitars[2].ProductionYear)</div>
                <a class="icon delete">Delete</a>
            </div>
        </div>