ASP.NET MVC 5:EditorTemplate中的Datepicker错误,无法多次显示

时间:2015-05-07 09:43:40

标签: c# jquery asp.net asp.net-mvc jquery-ui

EditorTemplate DateTime,我正在使用JQueryUI datepicker。当我要编辑多个日期时,只显示第一个日期选择器。

版本:

<package id="jQuery" version="2.1.3" targetFramework="net45" />
<package id="jQuery.UI.Combined" version="1.11.4" targetFramework="net45" />
<package id="Microsoft.AspNet.Mvc" version="5.2.2" targetFramework="net45" />

../查看/共享/ EditorTemplates / DateTime.cshtml:

@model DateTime?
@Html.TextBox("", (Model.HasValue ? Model.Value.ToShortDateString() :
string.Empty), new
{
    @class = "form-control-static",
    @id = "datepick"
})

../ Views / Shared / _Layout.cshtml - script:

<script>
    $(function () {
        $("#datepick").datepicker({
            showWeek: true,
            firstDay: 1,
            monthNames: ["Januar", "Februar", "Marts", "April", "Maj", "Juni", "Juli", "August", "September", "Oktober", "November", "December"],
            dateFormat: "dd-mm-yy",
            changeYear: true
        });
    });
</script>

我尝试编辑的视图:

<div class="form-group">
    @Html.LabelFor(model => model.StartDateTime, new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @Html.EditorFor(model => model.StartDateTime)
        @Html.ValidationMessageFor(model => model.StartDateTime, "", new { @class = "text-danger" })
    </div>
</div>

<div class="form-group">
    @Html.LabelFor(model => model.EndDateTime, new {@class = "control-label col-md-2"})
    <div class="col-md-10">
        @Html.EditorFor(model => model.EndDateTime)
        @Html.ValidationMessageFor(model => model.EndDateTime, "", new {@class = "text-danger"})
    </div>
</div>

如上所述,只有第一个正在运作。我在谷歌上找不到任何帮助我的东西。希望你能。

1 个答案:

答案 0 :(得分:2)

这种情况正在发生,因为idStartDateTime具有相同的EndDateTime。请参阅有关select an item using an id or class

的更多信息
  

由于ID是唯一的,因此该表达式始终选择零个或一个元素,具体取决于是否存在具有指定ID的元素。

要解决此问题,您必须使用class,因为您可以选择多于1个元素

您的代码将会发生类似的变化:

<强> ../查看/共享/ EditorTemplates / DateTime.cshtml:

@model DateTime?
@Html.TextBox("", (Model.HasValue ? Model.Value.ToShortDateString() :
string.Empty), new
{
    @class = "form-control-static datepick",
})

../ Views / Shared / _Layout.cshtml - script:

<script>
    $(function () {
        $(".datepick").datepicker({
            showWeek: true,
            firstDay: 1,
            monthNames: ["Januar", "Februar", "Marts", "April", "Maj", "Juni", "Juli", "August", "September", "Oktober", "November", "December"],
            dateFormat: "dd-mm-yy",
            changeYear: true
        });
    });
</script>