C# 允许使用 PhoneAttribute 或 UrlAttribute 标记的字段为空字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19135504/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 14:12:04  来源:igfitidea点击:

Allow empty strings for fields marked with PhoneAttribute or UrlAttribute

c#entity-frameworkvalidationef-code-firstdata-annotations

提问by Nu-hin

I'm using CodeFirst Entitty framework 5. I have a class representing a user.

我正在使用 CodeFirst Entitty 框架 5。我有一个代表用户的类。

public class User
{
    [Key]
    public int UserId { get; set; }

    [Url]
    [DataType(DataType.Url)]
    [Required(AllowEmptyStrings= true)]
    public string WebSite { get; set; }

    [Phone]
    [DataType(DataType.PhoneNumber)]
    [Required(AllowEmptyStrings = true)]
    public string Phone { get; set; }

    [Phone]
    [DataType(DataType.PhoneNumber)]
    [Required(AllowEmptyStrings = true)]
    public string Fax { get; set; }
}

I like the validation mechanics for Phoneand Urlattributes a lot, but unfortunately validation fails when fields marked with these attributes are empty strings which I actually want to allow. [Required(AllowEmptyStrings = true)]doesn't seem to work with Phoneor Urlattributes. The same seems to apply to some other DataAnnotations attributes like EmailAddress.

我非常喜欢PhoneUrl属性的验证机制,但不幸的是,当标有这些属性的字段是我真正想要允许的空字符串时,验证会失败。[Required(AllowEmptyStrings = true)]似乎不适用于PhoneUrl属性。这似乎同样适用于其他一些 DataAnnotations 属性,例如EmailAddress.

Is there a way to allow empty strings for fields marked with such attributes?

有没有办法允许标有此类属性的字段为空字符串?

采纳答案by Neil Laslett

Validation attributes like [Phone]and [EmailAddress]will check any non-null string values. Because the stringtype is inherently nullable, empty strings passed to the ModelBinder are read as null, which passes the validation check.

验证属性像[Phone]并且[EmailAddress]将检查任何非空字符串值。因为string类型本质上是可为空的,所以传递给 ModelBinder 的空字符串被读取为null,从而通过了验证检查。

When you add the [Required]attribute, the string becomes effectively non-nullable. (If using Code First, EF will script a non-nullable database column.) The ModelBinder will now interpret a blank value as String.Empty- which will fail the attribute validation check.

当您添加该 [Required]属性时,该字符串实际上变为不可为空。(如果使用 Code First,EF 将编写不可为空的数据库列的脚本。)ModelBinder 现在会将空白值解释为String.Empty- 这将使属性验证检查失败。

So there is no way to allow emptystrings with validation attributes, but you can allow nullstrings. All you need to do is remove the [Required]attribute. Blank values will be nulland non-blank values will be validated.

所以没有办法允许带有验证属性的字符串,但你可以允许字符串。您需要做的就是删除该[Required]属性。空白值将null被验证,非空白值将被验证。

In my case, I am importing records from a CSV file, and had this problem because I was skipping the normal ModelBinder. If you are doing something unusual like this, be sure to include a manual check before saving to your data model:

就我而言,我正在从 CSV 文件导入记录,但遇到此问题是因为我跳过了普通的 ModelBinder。如果您正在做这样的不寻常的事情,请确保在保存到您的数据模型之前包括手动检查:

Email = (record.Email == String.Empty) ? null : record.Email

回答by user5791468

Use following two data annotations:

使用以下两个数据注释:

[Required(AllowEmptyStrings = true)]
[DisplayFormat(ConvertEmptyStringToNull = false)]