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

标签 c# asp.net-core blazor blazor-server-side

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

    <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>

这是一些代码的高度编辑示例。不打算显示实际存在的内容。只是为了说明。

最佳答案

我猜你需要的是 ObjectGraphDataAnnotationsValidator 组件,它可以验证复杂类型。

这是 link to a simple sample

Here's a link Blazor 团队的类定义和示例

希望这可以帮助...

关于c# - 服务器端 Blazor 中单个 EditForm 中的多个模型验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59957025/

相关文章:

c# - 使用Blazor上传文件的最佳方法是什么?

c# - UWP 中 CollectionDataContract 的 DataContractSerialzer 错误

c# - 静态 XML 的缓存问题

c# - IdentityUser 如何验证旧密码?

oauth - 系统.IO.FileNotFoundException : 'Microsoft.Framework.ApplicationHost'

c# - .NET Core 3.1 中是否存在 HTTPResponse 响应

C# 如何单元测试/验证类提供的 API 是否是线程安全的?

c# - Request.IsAjaxRequest 不工作 mvc

c# - 从 asp.net Controller 中删除 Try/Catch 语句

blazor - 父组件未在 Blazor Web assembly 中更新