2012-06-22 45 views
1

我已經使用jQuery UI Helpers日期選擇器使用以下代碼。模型綁定到日期選擇器jQuery UI在asp.net mvc 3

<div> 
     <label for="date">Select a date:</label> @Html.JQueryUI().Datepicker("date") 
    </div> 

但我怎樣才能用這個幫手來綁定同一個模型:

<div class="editor-label"> 
     @Html.LabelFor(model => model.BirthDate) 
    </div> 
    <div class="editor-field"> 
     @Html.EditorFor(model => model.BirthDate) 
     @Html.ValidationMessageFor(model => model.BirthDate) 
    </div> 

因此,該值將被保存到數據庫中。

我已經在數據庫中使用的FormCollection像下面保存出生日期的崗位價值:

[HttpPost] 
     public ActionResult Edit(User user,FormCollection PostData) 
     { 
      if (ModelState.IsValid) 
      { 
       db.Users.Attach(user); 
       user.BirthDate = DateTime.Parse(PostData["date"]); 
       UpdateModel(user); 
       //db.ObjectStateManager.ChangeObjectState(user, EntityState.Modified); 
       db.SaveChanges(); 
       return RedirectToAction("Details"); 
      } 
      ViewBag.RoleID = new SelectList(db.Roles, "ID", "Name", user.RoleID); 
      return View(user); 
     } 

回答

1

嘗試使用這樣的:

<script> 
    $(function() { 
     $("#BirthDate").datepicker(); 
    }); 
</script> 
+0

感謝,它顯示日期選擇器。現在,不必在控制器上使用FormCollection – CodeManiac

+0

Hello Kapil,在這裏,DatePicker採用默認時間值(6/6/2012 12:00:00 AM)但我需要刪除時間並只顯示日期。雅,它可以使用'ToShortDateString()'完成。但是如何使用這個函數,因爲我有'@ Html.DisplayFor(model => model.BirthDate)'來顯示出生日期。我也看過數據庫,它只顯示日期而不顯示時間。我只需要顯示日期。我是mvc 3的新手,對於單一的輕微修改真的很迷惑。 – CodeManiac