我使用EntityFramework CodeFirst MVC3。在我的模型我定義了一個可爲空的屬性是這樣的:我的模型的可空屬性顯示在我的創建視圖
public class PostFullViewModel
{
public int PostID { get; set; }
...
public DateTime? PublishDate { get; set; }
...
}
這裏是我創建動作控制器:
public ActionResult Create()
{
PostCreateViewModel viewModel = new PostCreateViewModel
{
PostToCreate = new PostFullViewModel(),
Authors = m_AccountBusiness.GetAllUsers().Select(x => new SelectListItem { Text = x.UserName, Value = x.UserID.ToString() })
};
return View(viewModel);
}
在我創建視圖:
@model PostCreateViewModel
@using (Html.BeginForm())
{
@Html.LabelFor(model => model.PostToCreate.PublishDate)
@Html.EditorFor(model => model.PostToCreate.PublishDate)
...
}
我就得到了一個錯誤EditorFor line:
The model item passed into the dictionary is null, but this dictionary requires a non-null model item of type 'System.DateTime'.
如何讓我的創作視圖顯示PublishDate爲空?
謝謝。
其中從PostToCreate財產?你能詳細說明嗎? – Ray
我更新了我的問題以顯示創建操作控制器。 – Bronzato