我有一个简单的MudBlaczor MudSelect列表
<MudSelect T="int" Label="Choose Coverage Type"
AnchorOrigin="Origin.BottomCenter" Variant="Variant.Outlined"
Margin="Margin.Dense" For="@(() => newLink.CoverageType)">
<MudSelectItem Value="0">No Coverage Selected</MudSelectItem>
<MudSelectItem Value="1">Auto</MudSelectItem>
<MudSelectItem Value="2">Cyber</MudSelectItem>
<MudSelectItem Value="3">D&O/EPL</MudSelectItem>
<MudSelectItem Value="4">General Liability</MudSelectItem>
<MudSelectItem Value="5">General Risk Management</MudSelectItem>
<MudSelectItem Value="6">Property</MudSelectItem>
</MudSelect>
在我的课堂上,我有用于验证的DataAnnotations
//[RegularExpression ("^[1-6]$", ErrorMessage="A Coverage Type is Required")]
[Required]
public int CoverageType { get; set; }
我可以在简单的文本框上进行验证,但如果下拉列表中的第一个项目被选中,我希望出现验证错误;未选择覆盖范围";
有人知道如何使用MudBrazor控件吗?我在他们的网站上没有找到任何东西。
感谢的帮助
您可以尝试使用MudForm
组件验证字段。
示例:
https://try.mudblazor.com/snippet/QYQcaMkIrMqCiOQf
@using System.Text.RegularExpressions
@using System.ComponentModel.DataAnnotations
<MudForm @ref="form" class="my-3">
<MudSelect
T="int"
Label="Choose Coverage Type"
AnchorOrigin="Origin.BottomCenter"
Variant="Variant.Outlined"
Margin="Margin.Dense"
For="@(() => newLink.CoverageType)">
<MudSelectItem Value="0">No Coverage Selected</MudSelectItem>
<MudSelectItem Value="1">Auto</MudSelectItem>
<MudSelectItem Value="2">Cyber</MudSelectItem>
<MudSelectItem Value="3">D&O/EPL</MudSelectItem>
<MudSelectItem Value="4">General Liability</MudSelectItem>
<MudSelectItem Value="5">General Risk Management</MudSelectItem>
<MudSelectItem Value="6">Property</MudSelectItem>
</MudSelect>
</MudForm>
<MudButton Variant="Variant.Filled" Color="Color.Primary" OnClick="@(()=>form.Validate())">Validate</MudButton>
<MudButton Variant="Variant.Filled" Color="Color.Error" OnClick="@(()=>form.Reset())">Reset</MudButton>
<MudButton Variant="Variant.Filled" OnClick="@(()=>form.ResetValidation())">Reset Validation</MudButton>
@code {
private YourModel newLink = new YourModel();
private MudForm form;
class YourModel
{
[RegularExpression ("^[1-6]$", ErrorMessage="A Coverage Type is Required")]
[Required]
public int CoverageType { get; set; }
}
}