2016-10-11 50 views
1

我是初學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生日!!!爲什麼?

+0

請添加您的形式,控制層和數據層的代碼了。 – Sefa

+0

你是否將所有其他字段放入數據庫? – Emil

+0

是的,所有數據都是正確的,除了生日 –

回答

0

,我發現我的問題 我已經忘記了生日在[綁定]屬性

public ActionResult Create([Bind(Include = "BirthDay,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"); 
     } 
+0

所以就像我說的包括失蹤的生日 – Emil

+2

不要使用綁定,爲數據創建模型對象。 – Sousuke