【发布时间】:2021-10-02 19:56:27
【问题描述】:
在使用 blazor 时,创建可重用的自定义组件很重要;这是使用 Blazor 的要点之一。
我已经创建了一个基本的 Input 组件,它使用 Bootstrap 作为它的 CSS,如图所示。
@inherits InputBase<string>
<InputText type="@Type"
class="form-control"
id="@_id"
placeholder="@Placeholder"
@bind-Value="CurrentValue"
disabled="@Disabled"
@attributes="AdditionalAttributes"/>
/* Additional properties below for placeholder, disabled, type, etc. they're just string properties */
我把它放在一个 EditForm 中...
<EditForm Model="MyModel"
OnValidSubmit="ViewModel.CreateAsync">
<DataAnnotationsValidator />
<PrimaryInput @bind-Value="MyModel.Name"
Placeholder="Name..."/>
</EditForm>
问题是,这不会将“无效”类添加到项目中。有效和修改都会出现,但是一旦输入变为无效,它不会像使用非自定义组件那样将有效更改为无效。下面是我的模型,显示我已向模型添加了验证。
public class MyModel
{
[StringLength(5)]
[Required]
public string Name { get; set; }
}
下面是显示的图片:
这是 Blazor 生成的 html...
<input type="text" id="aa106d17-46d7-442c-be39-6c947f17186b" placeholder="Name..." aria-describedby="14b1189e-c3c5-4e1f-ae1d-330756147b44" class="form-control valid" aria-invalid="">
...如您所见,该类仍然有效,但已应用 aria-invalid 属性。以下是正在验证的非自定义组件输入的 HTML...
<input class="modified invalid" aria-invalid="">
...在这个上,无效。某些东西阻止了自定义组件上的类切换为无效。
【问题讨论】:
标签: c# html blazor blazor-webassembly