我想開發一個簡單的局部視圖: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