当前位置:网站首页>Blazor University (34)表单 —— 获得表单状态
Blazor University (34)表单 —— 获得表单状态
2022-06-29 00:39:00 【dotNET跨平台】
原文链接:https://blazor-university.com/forms/accessing-form-state/
获得表单状态
源代码[1]
有时,我们需要获得 <EditForm> 子内容中的表单状态。最常见的用途是当我们需要访问输入的 CSS 类时,指示输入是否被修改或有效/无效。
例如,如果我们使用 Bootstrap 创建一个带有 @ 符号的电子邮件输入控件,我们最终可能会得到如下所示的标记。
<div class="form-group">
<label for="EmailAddress">Email address</label>
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text">@@</span>
</div>
<InputText @bind-Value=MyContact.EmailAddress id="EmailAddress" class="form-control" type="email" />
</div>
</div>
然而,问题在于,当用户输入无效值时,CSS 无效类仅应用于 <InputText> 控件。

如果我们想将 CSS 无效类应用到输入组本身,我们可以使用从 <EditForm> 组件传递给我们的 EditContext。
<EditForm> 的 ChildContent 参数是 RenderFragment<EditContext>,这意味着 EditContext 实例通过名为 context 的变量(或我们告诉 Blazor 使用的任何别名)传递到其内部内容。有关更多信息,请参阅使用 RenderFragments 模板化组件[2]。
<EditForm [email protected] Context="CurrentEditContext">
<DataAnnotationsValidator />
<div class="form-group">
<label for="EmailAddress">Email address</label>
<div class="input-group @CurrentEditContext.FieldCssClass( () => MyContact.EmailAddress)">
<div class="input-group-prepend">
<span class="input-group-text">@@</span>
</div>
<InputText @bind-Value=MyContact.EmailAddress id="EmailAddress" class="form-control" type="email" />
</div>
</div>
</EditForm>第 1 行
使用
Context=语法,我们告诉 Blazor 在传入其EditContext时使用变量名称CurrentEditContext。第 6 行
使用
EditContext.FieldCssClass方法根据输入的状态(修改/有效/无效)获取正确的 CSS 类名称。

如果我们愿意,我们可以用一些简单的 CSS 隐藏生成的 <input> HTML 元素上的红色轮廓。
.input-group > input.invalid
{
outline: none;
}该 CSS 告诉浏览器,如果应用了无效类的 <input> HTML 元素直接由应用了 input-group CSS 类的 HTML 元素作为父元素,则该元素不应具有红色轮廓。

参考资料
[1]
源代码: https://github.com/mrpmorris/blazor-university/tree/master/src/Forms/AccessingFormState
边栏推荐
- Daily question 1: the number of numbers in the array
- Click hijack: X-FRAME-OPTIONS is not configured
- 手下两个应届生:一个踏实喜欢加班,一个技术强挑活,怎么选??
- Haskell configuring vs code development environment (june2022)
- Program environment and pretreatment
- Comparison between winding process and lamination process
- The magical zero knowledge proof can not only keep secrets, but also make others believe you!
- 每日一练:删除有序数组中的重复项
- MySQL 8.0 above reporting 2058 solution
- Matrix compression
猜你喜欢
随机推荐
674. longest continuous increasing sequence
每日一题: 数组中数字出现的次数
mysql 8.0以上报2058 解决方式
EditText listening focus
养老年金险是理财产品吗?预期收益在哪看?
Redis常用命令手册
由背景图缓存导致的canvas绘图跨域问题
10、YOLO系列
Encapsulation of JDBC connection and disconnection database
Is pension insurance a financial product? Where is the expected return?
Xiaobai's e-commerce business is very important to choose the right mall system!
Accessories and working process of machine vision system
FATAL ERROR: Could not find ./bin/my_print_defaults的解决办法
Redis是什么
Roson's QT journey 80 qurl class
Sampling with VerilogA module
Reasons for high price of optical fiber slip ring
搭建单机 nacos 负载均衡ribbon 轮询策略 权重2种方式
Oracle uses sqlloader to prompt sql*loader-406 Import failed but no error was reported
利用verilogA模块采样









