このチュートリアルでは、次のすべての手順に従いました。
バリデータークラスを作成しました
public class ProjectValidator : AbstractValidator<ProjectViewModel>
{
public ProjectValidator()
{
//RuleFor(h => h.Milestone).NotEmpty().WithName("Milestone");
RuleFor(h => h.Applications).NotNull().WithName("Application");
RuleFor(h => h.InitiativeName).NotNull().WithName("Business Aligned Priority");
RuleFor(h => h.BusinessDriverId).NotNull().WithName("Business Driver");
RuleFor(h => h.FundingTypeId).NotNull().WithName("Funding Type");
RuleFor(h => h.Description).NotEmpty().WithName("Description");
RuleFor(h => h.Name).NotEmpty().WithName("Project Name");
RuleFor(h => h.Sponsors).NotNull().WithName("Sponsors");
}
}
このバリデーターを特定するために、DTOに属性を設定します
[Validator(typeof(ProjectValidator))]
public class ProjectViewModel
{
}
しかし、フォームの投稿後、ModelStateエラーリストを確認すると、表示されるエラーはasp.net-mvcのデフォルトの検証から発生しています。
public ActionResult UpdateMe(ProjectViewModel entity)
{
Project existingProject = this.Repository.Fetch<Project>(entity.Id);
UpdateProperties(entity, existingProject);
var allErrors = ModelState.Values.SelectMany(v => v.Errors);
if (allErrors.Count() > 0)
{
なぜ流暢に話せないのかについての提案。バリデーター?? GUIで見たものの画像を下に追加しました
コードでバリデーターを直接呼び出すと、問題なく機能します。
ProjectValidator validator = new ProjectValidator();
ValidationResult result = validator.Validate(entity);