【发布时间】:2012-11-27 18:19:59
【问题描述】:
我在 ASP.NET MVC3 控制器中有以下代码:
public PartialViewResult GetCalendar(int? month, int? year)
{
var test = new DateTime((year.HasValue ? year.Value : 1), (month.HasValue ? month.Value : 1), 1);
return PartialView("Calendar", new DateTimeOffset(test));
}
我的视图模型是DateTimeOffset?
抛出异常的原因是什么?
【问题讨论】:
-
那么在失败的情况下
month和year的值是多少? (注意,null-coalescing 运算符会使这更简单,顺便说一句。) -
1 和 1,所以它就像 DateTime.MinValue
-
@JonSkeet 感谢您提醒有关空合并运算符
标签: asp.net-mvc exception datetimeoffset