当前位置:网站首页>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
边栏推荐
- Give you a project, how will you carry out performance testing (I)
- 请问基金是否靠谱,安全吗
- Summary of software testing cognition
- JVM工作原理介绍
- Test experience: how testers evolve from 0 to 1
- Daily question 1: remove elements
- Introduction to four MySQL engines
- MNIST handwritten numeral recognition demo based on pytorch framework
- 卷绕工艺与叠片工艺的对比
- Redis是什么
猜你喜欢
![[leetcode] 522. Longest special sequence II violence + double pointer](/img/88/3ddeefaab7e29b8eeb412bb5c3e9b8.png)
[leetcode] 522. Longest special sequence II violence + double pointer

6.28 学习内容

旋转接头安装使用注意事项
![[staff] pedal mark (step on pedal ped mark | release pedal * mark | corresponding pedal command in MIDI | continuous control signal | switch control signal)](/img/2b/e358b22d62ce6d683ed93418ff39fe.jpg)
[staff] pedal mark (step on pedal ped mark | release pedal * mark | corresponding pedal command in MIDI | continuous control signal | switch control signal)

Reasons for high price of optical fiber slip ring

12.物体检测Mask-Rcnn

The magical zero knowledge proof can not only keep secrets, but also make others believe you!

Daily practice: delete duplicates in the ordered array
![[image registration] improved SAR image registration based on sar-sift with matlab code](/img/69/4e78c6cef45b2e4d133222a4372e43.jpg)
[image registration] improved SAR image registration based on sar-sift with matlab code

每日一题: 数组中数字出现的次数
随机推荐
Comics | goodbye, postman! One stop collaboration makes apipost more fragrant!
由背景图缓存导致的canvas绘图跨域问题
12.物體檢測Mask-Rcnn
旋转接头安装使用注意事项
Introduction to four MySQL engines
Go1.18 new feature: discard strings Title Method, a new pit!
Operation level smart campus system source code smart campus applet source code + electronic class card + face recognition system
Résumé de Manon, 25 ans, diplômé en trois ans
The company has a new Post-00 test paper king. The old oilman said that he could not do it. He has been
这玩意叫跳表?
6.28 学习内容
Click hijack: X-FRAME-OPTIONS is not configured
674. longest continuous increasing sequence
Bug risk level
Sampling with VerilogA module
scp拷贝文件夹
Is l1-031 too fat (10 points)
[image denoising] matlab code for removing salt and pepper noise based on fast and effective multistage selective convolution filter
光纤滑环价格过高的原因
Redis是什么