可空对象必须在MVC4值 [英] Nullable object must have a value in MVC4

查看:259
本文介绍了可空对象必须在MVC4值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有可能为空或者设置时间日期属性一类。我为这个属性我创建的类型日期时间的字段可能为空或日期时间value.I我无法针对以检查日期设置与否。
里面Model类我有一个可为空datetime属性。

I have a Class which has an attribute which could be null or set to time date. I for this attribute i created a field of type Date time which could be null or Date time value.I am unable to check in view where date is set or not. Inside Model class I have a nullable DateTime attribute.

 public DateTime? CollectionDate { get; set; }

在我看来,我和数据是否被设置为任意值,或者是这样的。

In my view i and to check if data is set to any value or like this.

if( Model.CollectionDate.Value!=null){
        DateTime test = Model.CollectionDate.Value;
    }

但是,当我加载视图我给我的异常可空对象必须有一个值。
我怎么能检查在日期之前设置或不

But when i load view i give me exception Nullable object must have a value. how can i check in where date is set before or not

推荐答案

尝试

if( Model.CollectionDate.HasValue)

这篇关于可空对象必须在MVC4值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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