服务器端 Blazor 中单个 EditForm 中的多个模型验证

Aar*_*ord 6 c# asp.net-core blazor blazor-server-side

我有一个表单,它在一个 EditForm 中绑定到三个相关模型。我希望了解如何在同一提交上验证它们中的每一个。我已经能够成功验证单个模型,但我没有在任何地方看到有关如何验证多个模型的任何详细信息。有想法吗?

    <EditForm OnValidSubmit="@Save" EditContext="@EditContext">
    <div class="form-group">

                <input class="form-control" type="text" id="Title" @bind="@TargetUser.Title" />

                <InputText Id="OfficePhone" Class="form-control" @bind-Value="@TargetUser.OfficePhone"></InputText>
                <ValidationMessage For="@(() => TargetUser.OfficePhone)" />

                <input class="form-control" type="text" id="MiddleName" @bind="@TargetUser.MiddleName" />
        <div class="row row-padding">
            <h4>Seller Rates</h4>
        </div>
        <hr />
        <input type="number" step="0.01" id="HourlyRate" @bind="@UserRate.HourlyRate" class="form-control" />
        <input type="number" id="Salary" @bind="@UserRate.Salary" class="form-control" />
        <input type="number" step="0.01" id="OTRate" @bind="@UserRate.OTRate" class="form-control" />
        <input type="date" @bind="@UserRate.ValidFrom" id="ValidFrom" class="form-control"/>
        <input type="date" class="form-control" id="ValidTo" @bind="@UserRate.ValidTo" />

    <DataAnnotationsValidator />
    <ValidationSummary />
</EditForm>
Run Code Online (Sandbox Code Playgroud)

这是一些代码经过高度编辑的示例。无意展示实际存在的内容。只是为了说明。

Isa*_*aac 3

我想您这里需要的是 ObjectGraphDataAnnotationsValidator 组件,它可以验证复杂类型。

这是一个简单示例的链接

以下是Blazor 团队提供的类定义和示例的链接

希望这可以帮助...