Nullable DateTime with Html.EditorFor()ASP.NET MVC helper

时间:2012-02-20 18:43:43

标签: asp.net-mvc html-helper nullable

我的asp.net mvc viewmodel中有一个DateTime? StartDate { get; set; },我试图在我的视图中执行Html.EditorFor(x => x.StartDate)。我收到了一个错误

  

传递到字典中的模型项为null,但此字典需要“System.DateTime”类型的非null模型项。

如果没有在我的viewmodel上创建一个不可为空的代理属性,有什么好方法可以解决这个问题吗?

2 个答案:

答案 0 :(得分:4)

很可能你已经定义了这样的日期时间编辑器模板 - Views/Shared/EditorTemplates/Datetime.cshtml

@model Datetime
//etc

将模型更改为可以为空的日期时间

@model Datetime?
//etc

答案 1 :(得分:2)

这可能是您问题的答案:

<%: Html.EditorFor(model => model.SomeNullableDecimal, "NullableDecimalTemplate" )%>

信用: ASP.NET MVC 2 - Html.EditorFor a nullable type?