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

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

问题描述

我可能会过时,但一个原则我坚持是避免空值尽可能。

I may be out of date, but one principle I adhere to is avoid nulls as much as possible.

不过我发现是,在用户输入的对象我要救,如果不进入某些领域他们被指定为NULL。的属性强类型视图

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.

因此​​,而不是每个属性设置为空字符串,我怎么能自动设置文本框的每个窗体上默认为空字符串,而不是空?

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)]

所以每当有人张贴带有空文本字段的表单,这些都将是一个空字符串,而不是空...

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

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

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