在我INV_Assets
型號我有以下日期字段定義:在EditorFor中顯示[Date]值:'ToString'沒有重載方法需要1個參數?
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)]
public DateTime? acquired_date { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)]
public DateTime? disposed_date { get; set; }
[Required]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)]
public DateTime created_date { get; set; }
然後在我的Edit()
看法,我有以下的這些日期值的定義form-group
的:
<div class="form-group">
@*@Html.LabelFor(model => model.acquired_date, htmlAttributes: new { @class = "control-label col-md-2" })*@
<span class="control-label col-md-2">Acquired Date:</span>
<div class="col-md-10">
@Html.EditorFor(model => model.acquired_date, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.acquired_date, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@*@Html.LabelFor(model => model.disposed_date, htmlAttributes: new { @class = "control-label col-md-2" })*@
<span class="control-label col-md-2">Disposed Date:</span>
<div class="col-md-10">
@Html.EditorFor(model => model.disposed_date, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.disposed_date, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@*@Html.LabelFor(model => model.created_date, htmlAttributes: new { @class = "control-label col-md-2" })*@
<span class="control-label col-md-2">Created Date:</span>
<div class="col-md-10">
@Html.EditorFor(model => model.created_date, new { htmlAttributes = new { @class = "form-control", @Value = Model.created_date.ToString("yyyy-MM-dd") } })
@Html.ValidationMessageFor(model => model.created_date, "", new { @class = "text-danger" })
</div>
</div>
那麼現在,當created_date
有一個值時,我可以打開表單並且EditorFor
將正確顯示該值。但是,即使在數據庫中保存了值,acquired_date
和disposed_date
的EditorFor()
也會始終顯示爲mm/dd/yyyy
- 如果在編輯器仍顯示mm/dd/yyyy
時保存表格,數據庫中的值更改爲null
。
當我試圖指定acquired_date
和disposed_date
用(例)@Value = Model.disposed_date.ToString("yyyy-MM-dd")
爲htmlAttribute
,既Model.acquired_date.ToString("yyyy-MM-dd")
和Model.disposed_date.ToString("yyyy-MM-dd")
標誌作爲No overload method for 'ToString' takes 1 argument
...?
任何人都可以看到我做錯了什麼?
理想情況下,我想將輸入的值保存在用戶在EditorFor
中指定的Date
以及保存時的當前Time
。然後,如果用戶正在查看記錄,則我想在EditorFor
範圍內顯示格式爲mm/dd/yyyy
的值。
由於小號特芬,這是訣竅!是否有一種簡單的方法可以將當前時間與用戶指定的日期一起保存在控件上?目前指定的日期總是以'12:00:00 AM'保存。 – 2015-02-17 22:07:06
'[DataType(DataType.Date)]'顯示瀏覽器日期選擇器。你可以使用'[DataType(DataType。DateTime)]'顯示日期時間選擇器。你也可以在[這個答案]中使用該技術(http://stackoverflow.com/questions/27309260/populating-html5-datepicker-with-data-passed-from-a-model/27309928#27309928),但要注意'type =「date」'目前還不支持FireFox – 2015-02-17 22:12:44
嗯......當我將DataType.Date改爲DataType.DateTime時,我的空值顯示爲一個空的編輯框,而我的(例如)'created_date '顯示爲「2014-10-21」。沒有日期或日期時間選擇器似乎呈現? – 2015-02-17 22:24:43