我是初學MVC5的編程人員。 我與MVC5和Entity Framework 6 工作,我有一個類和一個DateTime屬性是這樣的:爲什麼總是在數據庫中,DateTime屬性爲空?
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}",ApplyFormatInEditMode = true)]
[Column(TypeName = "datetime2")]
public DateTime? BirthDay { get; set; }
我的控制器:
public ActionResult Create([Bind(Include = "MelliCode,EnCode,grade,quota,password,FName,Lname,shenasname,FatehrName,sex,tel,mobile,ResAddress,WorkAddress")] Expert expert)
{
if (ModelState.IsValid)
{
db.Experts.Add(expert);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(expert);
}
和我的看法是這樣的:
<div class="form-group">
@Html.LabelFor(model => model.BirthDay, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.BirthDay, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.BirthDay, "", new { @class = "text-danger" })
</div>
但是,在頁面運行後輸入數據庫中的個人信息插入爲null生日!!!爲什麼?
請添加您的形式,控制層和數據層的代碼了。 – Sefa
你是否將所有其他字段放入數據庫? – Emil
是的,所有數據都是正確的,除了生日 –