I may be out of date, but one principle I adhere to is avoid nulls as much as possible.
我可能已经过时了,但我坚持的一个原则是尽量避免无效。
However what I have found is that for a strongly typed view in which the user inputs the properties of an object I want to save, if some fields are not entered they are assigned as null.
然而,我发现,对于一个强类型视图,用户输入我想要保存的对象的属性,如果没有输入某些字段,它们将被赋值为null。
Then when you try to save the changes, the validation fails.
然后,当您尝试保存更改时,验证失败。
So rather than set each property to an empty string, how can I automatically set each TextBox on a form to default to an empty string rather than a null?
因此,与其将每个属性设置为空字符串,如何将表单中的每个文本框自动设置为空字符串而不是null?
4 个解决方案
#1
53
You could put the following attribute on your string-properties in your model:
您可以将以下属性放在模型中的字符串属性:
[DisplayFormat(ConvertEmptyStringToNull=false)]
So whenever someone posts a form with empty text-fields, these will be an empty string instead of null...
因此,每当有人用空文本字段发布一个表单时,这些都将是空字符串,而不是空字符串。
#2
2
To be honest, I'd say your coding methodology is out of date and flawed. You should handle all possibilities, it's not hard. That's exactly what string.IsNullOrEmpty(value);
is for.
老实说,我觉得你的编码方法过时了,而且有缺陷。你应该处理所有的可能性,这并不难。这正是string.IsNullOrEmpty(价值);是对的。
I'm guessing your validation logic is something like:
我猜你的验证逻辑是这样的:
if (value == string.Empty) { isValid = false; }
So it doesn't handle the null values. You should replace that check so it also checks for nulls.
它不处理空值。您应该替换那个检查,以便它也检查null。
string value1 = null;
string value2 = string.Empty;
string.IsNullOrEmpty(value1); // true
string.IsNullOrEmpty(value2); // true
#3
1
An alternative solution to using attributes on each model property, as described in the accepted answer, is using a custom model binder, see string.empty converted to null when passing JSON object to MVC Controller
在每个模型属性上使用属性的另一种解决方案,如被接受的答案中所描述的,是使用自定义模型绑定器,参见字符串。将JSON对象传递给MVC控制器时,将其转换为null
#4
1
I ran across this problem when dealing with an old service that requires empty strings. I created an extension method:
我在处理需要空字符串的旧服务时遇到了这个问题。我创建了一个扩展方法:
public static string GetValueOrDefault(this string str)
{
return str ?? String.Empty;
}
So you can use this when you want to make sure any strings that are null become empty
所以你可以用这个来确保任何空的字符串都是空的
yourString1.GetValueOrDefault();
yourString2.GetValueOrDefault();
#1
53
You could put the following attribute on your string-properties in your model:
您可以将以下属性放在模型中的字符串属性:
[DisplayFormat(ConvertEmptyStringToNull=false)]
So whenever someone posts a form with empty text-fields, these will be an empty string instead of null...
因此,每当有人用空文本字段发布一个表单时,这些都将是空字符串,而不是空字符串。
#2
2
To be honest, I'd say your coding methodology is out of date and flawed. You should handle all possibilities, it's not hard. That's exactly what string.IsNullOrEmpty(value);
is for.
老实说,我觉得你的编码方法过时了,而且有缺陷。你应该处理所有的可能性,这并不难。这正是string.IsNullOrEmpty(价值);是对的。
I'm guessing your validation logic is something like:
我猜你的验证逻辑是这样的:
if (value == string.Empty) { isValid = false; }
So it doesn't handle the null values. You should replace that check so it also checks for nulls.
它不处理空值。您应该替换那个检查,以便它也检查null。
string value1 = null;
string value2 = string.Empty;
string.IsNullOrEmpty(value1); // true
string.IsNullOrEmpty(value2); // true
#3
1
An alternative solution to using attributes on each model property, as described in the accepted answer, is using a custom model binder, see string.empty converted to null when passing JSON object to MVC Controller
在每个模型属性上使用属性的另一种解决方案,如被接受的答案中所描述的,是使用自定义模型绑定器,参见字符串。将JSON对象传递给MVC控制器时,将其转换为null
#4
1
I ran across this problem when dealing with an old service that requires empty strings. I created an extension method:
我在处理需要空字符串的旧服务时遇到了这个问题。我创建了一个扩展方法:
public static string GetValueOrDefault(this string str)
{
return str ?? String.Empty;
}
So you can use this when you want to make sure any strings that are null become empty
所以你可以用这个来确保任何空的字符串都是空的
yourString1.GetValueOrDefault();
yourString2.GetValueOrDefault();