我想开发一个简单的局部视图:Asp.Net MVC可空<T>模型局部视图
@model Nullable<DateTime>
@if (Model == null)
{
<span>Active</span>
}
else
{
<span>Resigned on @Model.GetValueOrDefault().ToShortDateString()</span>
}
,并从另一种观点认为使用它:
@Html.Partial("ViewTemplates/UserStatus",user.ResignDate)
视图完美的作品,除非它是一个Nullable<T>
。
我收到的错误:
当然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]'.
中user.ResignDate是一个可空的日期时间。
任何想法?
感谢
感谢archil,但我没有在调用页面模型(我在Model.Users和呈现用户属性迭代)。有任何想法吗? – 2013-03-22 11:44:47
我的代码只是一个猜测:)。你可以做 '@ Html.DisplayFor(model => user.ResignDate,“UserStatus”)',但正如我所提到的,只有当'UserStatus'视图被放置在'DisplayTemplates'文件夹中 – archil 2013-03-22 11:53:13
非常感谢@archil! – 2013-03-22 11:57:00