ASP.NET MVC转换为null,零长度字符串 [英] ASP.NET MVC Converting null to zero-length string

查看:184
本文介绍了ASP.NET MVC转换为null,零长度字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用MVC 3,并试图获得字段留白的零长度字符串被发送到数据库中,而不是空值。这可能与数据注解的属性呢?

I'm using MVC 3 and attempting to get fields left blank to be sent to the database as zero-length strings, instead of nulls. Is this possible to do with data annotation attributes?

如果没有,什么是最合适的地方,从空转换?模型验证过程是什么呢?

If not, what is the most appropriate place to convert from nulls? Is it during model validation?

推荐答案

我不会在验证模型做到这一点,但可能不是绑定(甚至在模型本身)。

I wouldn't do this in a validator, but probably in model binding instead (or even in the model itself).

通常情况下,在我的模型类,我把我的字符串属性默认为空字符串,并在他们的制定者,我转换为空值空字符串。

Often, in my model classes, I set my string properties to default to an empty string, and in their setters, I convert nulls to empty strings.

这是kindof痛苦一遍又一遍写这个东西的重复,但它是如此好得多不必须处理空值。

It's kindof a pain to write this repetitive stuff over and over, but it's so much nicer to not have to deal with nulls.

这篇关于ASP.NET MVC转换为null,零长度字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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