[英]Asp.Net MVC Partial view with Nullable<T> model
I'm trying to develop a simple partial view: 我正在尝试开发一个简单的局部视图:
@model Nullable<DateTime>
@if (Model == null)
{
<span>Active</span>
}
else
{
<span>Resigned on @Model.GetValueOrDefault().ToShortDateString()</span>
}
and use it from another view: 并从另一个视图使用它:
@Html.Partial("ViewTemplates/UserStatus",user.ResignDate)
The view works perfectly unless it is a Nullable<T>
. 视图效果很好,除非它是Nullable<T>
。
I receive the error: 我收到错误:
The model item passed into the dictionary is of type 'System.Collections.Generic.List
1[Models.UserModel]', but this dictionary requires a model item of type 'System.Nullable
1[System.DateTime]'. 传递到字典中的模型项的类型为'System.Collections.Generic.List1[Models.UserModel]', but this dictionary requires a model item of type 'System.Nullable
1 [System.DateTime]'1[Models.UserModel]', but this dictionary requires a model item of type 'System.Nullable
。
Of course user.ResignDate is a nullable date time. 当然user.ResignDate是一个可以为空的日期时间。
Any ideas? 有任何想法吗?
Thanks 谢谢
If model parameter passed to Partial method (user.ResignDate) is null, then asp.net-mvc passes caller view's model to Partial view, and you get error. 如果传递给Partial方法的模型参数(user.ResignDate)为null,则asp.net-mvc将调用者视图的模型传递给Partial视图,并且您会收到错误。
If you followed conventions ans placed UserStatus into DisplayTemplates folder, I would have reccommeded you to replace Partial call with DisplayFor method 如果您遵循约定将UserStatus置于DisplayTemplates文件夹中,我会告诉您用DisplayFor方法替换部分调用
@Html.DisplayFor(model => model.user.ResignDate, "UserStatus")
请检查user.ResignDate的数据类型,它应该是DateTime不可为空的DateTime
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.