如何使用数据注释属性类使表单中的空字符串失败? [英] How Can I Use Data Annotations Attribute Classes to Fail Empty Strings in Forms?

查看:153
本文介绍了如何使用数据注释属性类使表单中的空字符串失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在表单中要求一个文本输入字段,这意味着表单中需要包含某些内容.但是,向我的模型添加[Required]标记无效.具体来说,请查看用户名"属性:

I was trying to require a text input field in a form, which implies that there needs to be something in the form. However, adding a [Required] tag to my model wasn't working. Specifically, look at the User Name property:

public class ColumnWidthMetaData {
    [DisplayName("Column Name")]
    [Required]
    public string ColName { get; set; }

    [DisplayName("Primary Key")]
    public int pKey { get; set; }

    [DisplayName("User Name")]
    [Required]
    public string UserName { get; set; }

    [DisplayName("Column Width")]
    [Required]
    public int Width { get; set; }
}

这允许空字符串通过模型验证,并最终在尝试为用户名插入NULL值时引发数据库错误.

This allows empty strings to get past model validation and eventually a database error is thrown when it attempts to insert a NULL value for User Name.

如何更改这些属性,以便它们执行应有的作用?

How can I change these attributes so they do what they seem like they should do?

推荐答案

经过大量的Google搜索并查看了Stackoverflow之后,我一无所有.

After a lot of Googling and looking on Stackoverflow, I had nothing.

我去了MSDN,并查看了系统.ComponentModel.DataAnnotations命名空间.

I went to MSDN and looked at the System.ComponentModel.DataAnnotations Namespace.

我在那里更仔细地查看了Required属性,并注意到了

There I looked more closely at the Required attribute, and noticed the AllowEmptyStrings property. Setting this to false tells the attribute not to allow any empty strings, which I would have assumed was the default behavior, seeing as how the point of Required is to require that something be entered, and an empty string indicates that nothing was entered.

但是,这不能解决问题,因为默认情况下,空字符串被强制为null,它们不是空字符串,因此是允许的.这又是荒谬的,因为Required应该测试是否输入了某些内容,而null表示没有输入任何内容.但是,AllowEmptyStrings页面具有指向DisplayFormAttribute的属性

This doesn't solve the problem though, as by default empty strings are coerced to null, which are not empty strings, and are therefore allowed. Once again this is absurd, as Required is supposed to test if something was entered, and null indicates nothing was entered. However, the AllowEmptyStrings page has a link to DisplayFormAttribute's Property ConvertEmptyStringsToNull. If you set this to false, then empty strings will remain empty strings, and then the required tag will not allow them.

所以,这是解决方法:

public class ColumnWidthMetaData {
    [DisplayName("Column Name")]
    [Required(AllowEmptyStrings=false)]
    [DisplayFormat(ConvertEmptyStringToNull=false)]
    public string ColName { get; set; }

    [DisplayName("Primary Key")]
    public int pKey { get; set; }

    [DisplayName("User Name")]
    [Required(AllowEmptyStrings=false)]
    [DisplayFormat(ConvertEmptyStringToNull=false)]
    public string UserName { get; set; }

    [DisplayName("Column Width")]
    [Required]
    public int Width { get; set; }
}    

这篇关于如何使用数据注释属性类使表单中的空字符串失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆