【发布时间】:2021-06-02 09:17:58
【问题描述】:
我在 Blazor 服务器应用程序中有一个带有 Email 属性的模型:
class Inputs
{
[Required]
[EmailAddress]
public string Email { get; set; }
}
我也有这个帮助类(用于上下文):
public class FormModel<T> where T : class, new()
{
private readonly EditContext EditContext;
public FormModel()
{
Model = new T();
EditContext = new EditContext(Model);
}
public T Model
{
get;
init;
}
public EditContext Validation => EditContext;
}
当我对值 "admin" 运行验证时,我会返回 true 而不是 false:
private readonly FormModel<Inputs> Form = new();
private void Submit() {
// Debugger: Form.Model.Email = "admin"
bool isValid = Form.Validation.Validate(); // true - not what I expect for "admin"
if (!isValid) {
return;
}
// ... code that should not be currently hit, but is ...
}
当根本没有提供Email 时也会发生同样的事情(尽管我不确定[Required] 是否认为空字符串是非答案?):
我在应用程序的其他地方使用了[EmailAddress],验证按预期工作。
什么可能导致验证失败?
【问题讨论】:
标签: c# .net razor blazor .net-5