我有一个简单的帐单创建表格。我在此表单上有两个日期字段。
[DataType(DataType.Date)]
public DateTime FromDate { get; set; }
[DataType(DataType.Date)]
public DateTime ToDate { get; set; }
在视图上,我有两个字段(日期选择器):
<div class="form-group">
<label class="col-sm-2 control-label">From date:</label>
<div class="col-sm-6">
@Html.TextBoxFor(m => m.FromDate, new { @class = "form-control date-pickerFrom", placeholder = "Enter Drop-off date here..." })
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">To date:</label>
<div class="col-sm-6">
@Html.TextBoxFor(m => m.ToDate, new { @class = "form-control date-pickerTo", placeholder = "Enter Drop-off date here..." })
</div>
</div>
在视图末尾,我具有用于两个日期选择器的JavaScript函数:
$(function () {
$('.date-pickerFrom').datepicker({
dateFormat: "dd/mm/yy"
});
$('.date-pickerTo').datepicker({
dateFormat: "dd/mm/yy"
});
});
当我确认自己的表格时,我只能在控制器中的“起始日期”字段中获得正确的日期值。 “迄今为止”值始终为01/01/0001。知道我在做什么错吗?
谢谢您,祝您愉快!
答案 0 :(得分:0)
将视图更改为:
<div class="form-group">
<label class="col-sm-2 control-label">From date:</label>
<div class="col-sm-6">
@Html.EditorFor(m => m.FromDate, new { htmlAttributes = new { @id = "datepickerFrom", @class = "form-control", @placeholder = "Enter Drop-off date here..."}})
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label">To date:</label>
<div class="col-sm-6">
@Html.EditorFor(m => m.ToDate, new { htmlAttributes = new { @id = "datepickerTo", @class = "form-control", @placeholder = "Enter Drop-off date here..."}})
</div>
</div>
并在您的JavaScript中以以下方式调用它们:
$(document).ready(function () {
$('#datepickerFrom').datepicker({
dateFormat: "dd/mm/yy"
});
$('#datepickerTo').datepicker({
dateFormat: "dd/mm/yy"
});
});
为每个日期选择器分配一个id
,它应该对您有用。