0

我在服务器端 blazor 上绑定选择时遇到问题。传递的onchange事件是选项文本(标签)。这是我的选择元素:

<div class="form-group row">
     <div class="form-group col-md-6">
     <label>Role</label>
            <select class="form-control form-control form-control-sm"
                    placeholder="Role"
                    disabled="@IsReadOnly"
                    @onchange="(e) => RoleChanged(e)">
                        <option value="">Select Role...</option>
                        <option value="Member">Member</option>
                        <option value="Admin">Admin</option>
                        <option value="Pioneer">Pioneer</option>
                        <option value="Retailer">Retailer</option>
            </select>
            <ValidationMessage For="@(() => Model.Role)" class="row" />
</div>

在调试RoleChanged方法时

选项文本值

触发事件时,它将选项文本作为值。客户端验证也没有触发

客户端验证

4

1 回答 1

1

重构您的代码以使用表单组件:

<div class="form-group row">
     <div class="form-group col-md-6">
     <label>Role</label>
            <InputSelect class="form-control form-control form-control-sm"
                    placeholder="Role"
                    disabled="@IsReadOnly" Value="Model.Role" ValueChanged="RoleChanged" ValueExpression="@(() => Model.Role)">
                        <option value="">Select Role...</option>
                        <option value="Member">Member</option>
                        <option value="Admin">Admin</option>
                        <option value="Pioneer">Pioneer</option>
                        <option value="Retailer">Retailer</option>
            </InputSelect >
            <ValidationMessage For="@(() => Model.Role)" class="row" />
</div>
// an async method must return Task or ValueTask
private async Task RoleChanged(string value)
{
   // your logic
}
于 2020-05-13T14:14:26.517 回答