我正在使用datetimepicker,我想禁用控制器中的特定日期及其之前的所有日期。
那么如何将日期从控制器传递到 datetimepicker?
这就是我的意思:
MRecord firstMR = db.MRecords.Where(x => x.AID == dSum.AircraftID).OrderBy(x => x.DateEntered).FirstOrDefault();
<script type="text/javascript">
$(function () {
$('#datetimepicker5').datetimepicker({
disabledDates: [
// how do I disable all dates before and including firstMR.DateEntered?
]
});
});
</script>
是否有可能在视图中完成这一切?我正在使用剃须刀。
任何帮助表示赞赏。
更新
我在 Razor View 中尝试这个:
@{
ViewBag.Title = "Create";
var firstMR = new ALogSummary.Models.MRecord();
using (var db = new AviationLogSummary.Models.ALogsEntities())
{
firstMR = db.MRecords.Where(x => x.AID == Model.AID).OrderBy(x => x.DateEntered).FirstOrDefault();
}
}
<div id="datetimepicker2" class="input-group date">
@Html.EditorFor(model => model.Day, new { htmlAttributes = new { id = "Day", @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Day, "", new { @class = "text-danger" })
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
<script type="text/javascript">
$(function () {
$('#datetimepicker2').datetimepicker({
format: 'MM/DD/YYYY HH:mm',
minDate: [@firstMR.DateEntered.Date]
});
});
</script>
我在调试时收到此错误:
在http://localhost:xxxxx/DSummaries/Create/7中的第 160 行第 49 列出现 JavaScript 严重错误\n\nSCRIPT1007: Expected ']'
更新 2:
<div id="datetimepicker2" class="input-group date" >
@Html.EditorFor(model => model.Day, new { htmlAttributes = new { id = "Day", @class = "form-control", data_mindate = firstMR.DateEntered.Date } })
@Html.ValidationMessageFor(model => model.Day, "", new { @class = "text-danger" })
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span>
</span>
</div>
脚本:
这是在它自己的 JS 文件中
$(function () {
$('#datetimepicker2').datetimepicker({
format: 'MM/DD/YYYY HH:mm',
minDate: $(this).data('mindate')
});
});
HTML 输出为:
<input class="form-control text-box single-line valid" id="Day" type="datetime" data-val-required="This field is required!" data-val="true" data-mindate="05/28/2016 00:00:00" data-val-date="This field: must be a date." value="06/28/2016 09:07"></input>
因此正在填充正确的思维定势,但我的脚本无法正常工作。