禁用特定日期及其之前的所有日期

Disable Specific Date and all Dates before it

我正在使用 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>

我在调试时收到此错误:

JavaScript critical error at line 160, column 49 in http://localhost:xxxxx/DSummaries/Create/7\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>

所以正在填充正确的 mindate,但我的脚本不工作。

不使用 disabledDates 选项,而是使用 minDate 选项来防止用户选择 minDate

之前的日期
$('#datetimepicker2').datetimepicker({
    format: 'MM/DD/YYYY HH:mm',
    minDate: '@firstMR.DateEntered.Date'
});

由于此脚本位于外部 js 文件中(并且不会在外部文件中解析 razor 代码),您可以使用元素上的 data-* 属性传递值

@Html.EditorFor(model => model.Day, new { htmlAttributes = new { data_mindate = firstMR.DateEntered.Date, @class = "form-control" } })

请注意,不需要包含 id = "Day",因为 EditorFor() 方法已经添加了它(而且您似乎打错了字,因为脚本引用了带有 [=19 的元素=]).

然后在外部文件

var day = $('#Day')
day.datetimepicker({
    format: 'MM/DD/YYYY HH:mm',
    minDate: day.data('mindate')
});