如何设置TextBox空字符串的默认值而不是null [英] How to set default value of TextBox empty string instead of null

查看:55
本文介绍了如何设置TextBox空字符串的默认值而不是null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可能已经过时了,但我坚持的一个原则是尽可能避免空值.

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.

然后当您尝试保存更改时,验证失败.

Then when you try to save the changes, the validation fails.

因此,不是将每个属性设置为空字符串,而是如何自动将表单上的每个 TextBox 设置为默认为空字符串而不是 null?

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?

推荐答案

您可以将以下属性放在模型中的字符串属性上:

You could put the following attribute on your string-properties in your model:

[DisplayFormat(ConvertEmptyStringToNull=false)]

因此,每当有人发布带有空文本字段的表单时,这些将是一个空字符串而不是 null...

So whenever someone posts a form with empty text-fields, these will be an empty string instead of null...

这篇关于如何设置TextBox空字符串的默认值而不是null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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