无法提交带有子实体的 Blazor 表单

我有以下 2 个实体(为简洁起见省略了不相关的字段):

public class FacilityModel
{
    public long Id { get; set; }

    [Required]
    public InterfaceModel Interface { get; set; }
}

public class InterfaceModel
{
    public long Id { get; set; }

    [Required]
    [StringLength(50,ErrorMessage = "Name must be shorter than 50 characters")]
    
    public string Name { get; set; }
}

我的 AddFacility 页面中有以下 EditForm:

<EditForm Model="@_facility" OnValidSubmit="@(e => {if (_edit) UpdateFacility(); else InsertFacility(); })">
<DataAnnotationsValidator />
<ValidationSummary />
<div class="row">
    <div class="col-md-8">
        <div class="form-group">
            <label for="intfc" class="control-label">Interface</label>
            <InputSelect id="intfc" class="form-control" @bind-Value="_facility.Interface">
                @foreach (var intfc in Task.Run(() => _interfaceclient.GetallInterfaces()).Result)
                {
                    <option value="@intfc">@intfc.Name</option>
                }
            </InputSelect>
        </div>
    </div>
</div>

<div class="row">
    <div class="col-md-4">
        <div class="form-group">
            <input type="submit" class="btn btn-primary" value="Save" />
            <input type="button" class="btn btn-primary" @onclick="@Cancel" value="Cancel" />
        </div>
    </div>
</div>

页面在相应的下拉列表中正确显示了接口列表,但无论我做什么,表单验证都会出现“需要接口字段”。信息。我做错了什么,为什么所选接口的值没有正确保存在后备模型对象中?

编辑

我将拉取接口列表的代码移到 OnInitializedAsync() 方法中,感谢您的提示!我尝试将 InputSelect 字段中的密钥更改为 @intfc.Id 无济于事。我不知道如何将任何东西连接到验证过程中,因为我的理解是它直接在客户端发生在表单中?我很乐意在这里出错,我可以编写服务器端代码来自己处理 DataAnnotationsValidator 吗?

daihongjian8307 回答:无法提交带有子实体的 Blazor 表单

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/181.html

大家都在问