2012-04-09 72 views
0

與佈線向上日期選擇器日期選擇器不保存到數據庫

爲局部視圖的代碼

@model System.DateTime 
@Html.TextBox("", Model.ToString("dd/MM/yy"), new { @class = "date" }) 

爲視圖的代碼

@Html.TextBox("DateTime", (Model.AdvertStartDate.ToString()), new { @class = "date" }) 

腳本

<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/> 
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script> 
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script> 

問題用於datepick的代碼呃

<script type="text/javascript"> 
    $(document).ready(function() { 
     $('.date').datepicker 
     ({ 
      dateFormat: 'dd/mm/yy', 
      showStatus: true, 
      showWeeks: true, 
      currentText: 'Now', 
      autoSize: true, 
      gotoCurrent: true, 
      showAnim: 'blind', 
      highlightWeek: true 

     }); 
    }); 
</script> 

代碼模型

[Required] 
    public DateTime AdvertStartDate { get; set; } 

的日期插入文本框,但在數據庫中

您能否提供任何缺失沒有更新?

+0

嘗試選擇此日期「1/1/2012」一邊保存,這是否也行不通,我想我知道這個問題,但你先試試這個,讓我知道 – Yasser 2012-04-09 10:31:21

+0

仍然不起作用! – 2012-04-09 10:36:09

+0

嗯,好的,你如何保存日期填滿? – Yasser 2012-04-09 10:40:28

回答

1

我認爲由TextBox("DateTime",...)呈現的HTML <input>元素沒有正確的名稱(名稱將爲「DateTime」),因此在將數據發佈到數據時,它不會正確綁定到您的模型屬性AdvertStartDate服務器。

你的「局部視圖」看起來像一個EditorTemplate給我。在這種情況下,你應該能夠在你看來只是用這樣的:

@Html.EditorFor(model => model.AdvertStartDate) 

因爲AdvertStartDate類型爲DateTime包括的DatePicker的EditorTemplate應該用於渲染輸入元素。

如果EditorTemplate文件還有另一個名字比DateTime.cshtml - 像AnotherTemplateName.cshtml - 你可以明確地指定模板:

@Html.EditorFor(model => model.AdvertStartDate, "AnotherTemplateName") 
0

Datapicker是不是在MVC應用程序中實現了輕鬆的元素。

嘗試condsider接下來的事情:

  1. 確保從數據選擇器數據發送到服務器(datapicker應該有適當的名稱)
  2. 確保datapicker DATAFORMAT相同應用程序的數據格式(這可能與選定的文化有所不同) - 在這種情況下,數據被髮送到服務器,但並不總是有效的,因爲31.01會被轉換爲第31個月,日期1.
相關問題