[英]Nullable DateTime with Html.EditorFor() ASP.NET MVC helper
I have a DateTime? StartDate { get; set; }
我有一个
DateTime? StartDate { get; set; }
DateTime? StartDate { get; set; }
DateTime? StartDate { get; set; }
in my asp.net mvc viewmodel, and I tried to do Html.EditorFor(x => x.StartDate)
in my view. DateTime? StartDate { get; set; }
在我的asp.net MVC视图模型,并且我试图做Html.EditorFor(x => x.StartDate)
在我的视图。 I got an error 我有一个错误
The model item passed into the dictionary is null, but this dictionary requires a non-null model item of type 'System.DateTime'.
传递到字典中的模型项为null,但是此字典需要类型为'System.DateTime'的非空模型项。
Anything good way to get around this without creating a non-nullable surrogate property on my viewmodel? 有什么好方法可以解决此问题,而无需在我的viewmodel中创建不可为空的替代属性?
Most probably you have defined datetime editor template like this - Views/Shared/EditorTemplates/Datetime.cshtml
您很可能已经定义了这样的日期时间编辑器模板
Views/Shared/EditorTemplates/Datetime.cshtml
@model Datetime
//etc
Change model to take nullable datetime 更改模型以使用可为空的日期时间
@model Datetime?
//etc
This might be the answer to your issue: 这可能是您的问题的答案:
<%: Html.EditorFor(model => model.SomeNullableDecimal, "NullableDecimalTemplate" )%>
Credit: ASP.NET MVC 2 - Html.EditorFor a nullable type? 图片来源: ASP.NET MVC 2-HTML.Editor对于可为空的类型?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.