开拓者和FluentValidation

时间:2020-08-16 00:40:50

标签: blazor fluentvalidation

我一直在尝试使用Blazor和FluentValidation作为学习过程,但似乎连“ Hello World”都打不通!工作。

我开始了一个新项目,并通过NuGet添加了Fluent,它具有非常基本的数据类,验证器和索引剃须刀页面。

没有验证消息出现。我只是感到恐惧,“发生了未处理的异常。有关详细信息,请参阅浏览器开发工具。”在页面的底部。请帮我解决这个问题。

using FluentValidation;
namespace TestFluent.Data
{
    public class Employee
    {
        public string Name { get; set; }
    }

    public class EmployeeValidator:AbstractValidator<Employee>
    {
        public EmployeeValidator()
        {
            RuleFor(n => n.Name).NotEmpty().WithMessage("Please enter a name");
            RuleFor(n => n.Name).MaximumLength(30).WithMessage("Name must be 30 characters or less");
        }
    }
}

和Index.razor:

@page "/"
@using TestFluent.Data;

<h3>Header</h3>
<EditForm Model="@emp" OnValidSubmit="@HandleSubmit">
    <FluentValidationValidator />
    <ValidationSummary />
    <div class="form-group">
        <label class="col-form-label">Name:</label>
        <div class="col-md-6">
            <InputText @bind-Value="@emp.Name"></InputText>
            <ValidationMessage For="@(() => emp.Name)" />
        </div>
    </div>
    <button class="btn btn-success" type="submit">Save</button>
    <p><label>@msg</label></p>
</EditForm>

@code {
    string msg = "";
    Employee emp = new Employee();

    void HandleSubmit()
    {
        msg = "Success!";
    }
}

编辑:我关注了这段视频https://www.youtube.com/watch?v=DQxsQu9yg20

Dev工具错误:

[2020-08-16T01:20:44.379Z] Error: System.MissingMethodException: Method not found: 'System.Threading.Tasks.Task`1<FluentValidation.Results.ValidationResult> FluentValidation.IValidator.ValidateAsync(System.Object, System.Threading.CancellationToken)'.
    
           at Blazored.FluentValidation.EditContextFluentValidationExtensions.ValidateModel(EditContext editContext, ValidationMessageStore messages, IServiceProvider serviceProvider, IValidator validator)

   at System.Runtime.CompilerServices.AsyncMethodBuilderCore.Start[TStateMachine](TStateMachine& stateMachine)

   at Blazored.FluentValidation.EditContextFluentValidationExtensions.ValidateModel(EditContext editContext, ValidationMessageStore messages, IServiceProvider serviceProvider, IValidator validator)

   at Blazored.FluentValidation.EditContextFluentValidationExtensions.<>c__DisplayClass2_0.<AddFluentValidation>b__0(Object sender, ValidationRequestedEventArgs eventArgs)

   at Microsoft.AspNetCore.Components.Forms.EditContext.Validate()

   at Microsoft.AspNetCore.Components.Forms.EditForm.HandleSubmitAsync()

   at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task)

   at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle)

0 个答案:

没有答案