在asp.net核心中,是否将空字符串转换为NULL? [英] In asp.net core, are empty string converted to NULL?

查看:91
本文介绍了在asp.net核心中,是否将空字符串转换为NULL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个 form ,并且当 form 从视图发送到" 的值>控制器到 action方法 asp.net core 会将空字符串转换为 NULL 值吗?

Assuming I have a form, and the value of "" is selected when the form is sent from the view to the controller to the action method, will asp.net core convert the empty string to a NULL value?

如果我没有使 [required] 属性的布尔属性可为空,则会出现以下错误:值"无效.

If I do not make the Boolean property nullable for the [required] attribute, it gives me this error: The value '' is invalid.

这是否意味着:" 被评估为 NULL ,布尔属性不允许 NULL ,asp.net核心返回一个错误消息,您不能将空的 string 传递给 Model 属性,因为它不能为空,因为 asp.net core 会将空字符串转换为 NULL ?

Does this mean that: "" is evaluated as NULL, the Boolean property does not allow NULL, asp.net core returns a error saying that you can not pass a empty string to the Model property because it is not nullable because asp.net core converts the empty string to a NULL?

推荐答案

您需要了解的第一件事是,此处的选项"字段是类型的 bool(不是字符串) .

The first thing you need to understand is that the Options field here is the type of bool(not string).

它只能接收的内容为true或false或null ,无论您输入的是空字符串还是非true或false的字符串,它都将被识别为null

The only content it can receive is true or false or null, whether you enter an empty string or a string other than true or false, it will recognized as null.

Reuqired 的属性指示选项字段不能为空,因为 Options是布尔类型 ,因此输入空字符串后,该空字符串将转换为空值,并且由于reuqired属性的限制,该空字符串不能为空,因此会提醒您无效.

The attribute of Reuqired indicates that the Options field cannot be null,because Options is a bool type, so after you enter an empty string, the empty string is converted to a null value, and due to the restriction of the reuqired attribute, it cannot be null, so it will remind you invalid.

如果要允许选项"接收空值,则只需要删除reuqired属性即可.

If you want to allow Options to receive null values, you only need to remove the reuqired attribute.

在Required属性限制的前提下,我进行了代码测试,您可以参考:

In the premise of the Required attribute limitation, I did a code test, you can refer to:

这篇关于在asp.net核心中,是否将空字符串转换为NULL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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