1

我在視圖上有三個文本輸入。所有課程都有datepicker單個jQuery UI DatePicker不起作用

@model DateTime? 
@{ 
    string editText = Model.HasValue ? Model.Value.ToShortDateString() : ""; 
} 
@Html.TextBox("", editText, new { @class = "datepicker" }) 

這些都設置了日期選擇器在我_Layout模板下面的代碼:

$(function() { 
    $(".datepicker").datepicker({ 
     dateFormat: "yy/mm/dd" 
    }); 
}); 

現在

@Html.TextBox("Dummy", DateTime.Now, new { @class = "datepicker" }) 
@Html.EditorFor(model => model.StartDate) 
@Html.EditorFor(model => model.EndDate) 

底部的兩個被我DateTime編輯模板覆蓋彈出日期選擇器StartDate,只是完全沒有響應。它甚至不會改變幾個月,不用介意選擇一個日期。所有三個輸入都是相同的。無論StartDateEndDate被聲明爲不可爲空DateTime領域:

[Display(Name = "Start Date")] 
    public DateTime StartDate { get; set; } 

    [Display(Name = "End Date")] 
    public DateTime EndDate { get; set; } 

我使用jQuery 1.10.2和jQuery UI 1.10.3,無論是通過安裝的NuGet。我必須包括以下腳本才能使DatePickers正常工作:

<script src="http://code.jquery.com/jquery-migrate-1.1.1.js"></script> 

我完全受阻。

修訂: 錯誤顯示在JavaScript控制檯:

點擊每天產生錯誤「無法設置屬性未定義‘CURRENTDAY’」。

點擊上一個或下一個月份圖標產生「未定義無法讀取屬性‘設置’」

+0

那麼datepicker顯示爲EndDate或Dummy之類的任何其他輸入?反應遲鈍?還假設沒有Javascript控制檯錯誤? –

+0

@StevenV我引用了上面的自述:「StartDate的日期選擇器彈出,*僅*,完全沒有響應。」然而,有一些有趣的控制檯錯誤。請看我對這個問題的修正。 – ProfK

+3

當頁面上有重複的ID時,我遇到了那些Javascript錯誤。我會看看您呈現的HTML並查看頁面上是否有任何重複的「StartDate」ID。 –

回答

0

使用此鏈接獲取更多信息:http://jqueryui.com/datepicker/

,努力改變毫米MM

$(function() { 
    $(".datepicker").datepicker({ 
     dateFormat: "yy/MM/dd" 
    }); 
}); 

第二個答案:

嘗試使用此日期/時間選擇: http://tarruda.github.io/bootstrap-datetimepicker/http://www.eyecon.ro/bootstrap-datepicker/

我用它們兩個,併爲我工作。 希望這個援助。

+0

根據http://api.jqueryui.com/datepicker/#utility-formatDate「MM」和「mm」是有效的。我懷疑整個日期選擇器會停止工作。 –

+0

是的。但我發現了別的東西。希望這個幫助。 – SanRyu

+1

@SanRyu,其他地方或我的任何項目中的日期選擇器沒有任何問題。我寧願找出這裏出了什麼問題,而不是替換爲一個不正常的例子。 – ProfK

1

我想你沒有一個文本框的唯一ID? 日期選擇器需要一個唯一的ID。 要麼將​​其添加在JavaScript

$(function() { 
    $(".datepicker:not(.hasdatepicker)").uniqueId().datepicker({ 
     dateFormat: "yy/mm/dd" 
    }); 
}); 

或剃刀將​​其添加

@Html.TextBox("", editText, new { @class = "datepicker", id = string.Format("datepicker-{0:N}", Guid.NewGuid()) }) 
1

我使用的每個腳本文件的確切版本(jQuery的,jQuery的UI,jQuery的遷移),並使用你的代碼,發現沒有錯誤。它工作正常,沒有任何問題。

模態:

public class Test 
    { 
     [Display(Name = "Start Date")] 
     public DateTime StartDate { get; set; } 

     [Display(Name = "End Date")] 
     public DateTime EndDate { get; set; } 
    } 

編輯視圖:

@model MvcApplication2.Models.Test 

@{ 
    ViewBag.Title = "Test"; 
    Layout = "~/Views/Shared/_Layout.cshtml"; 
} 

<h2>Test</h2> 

@using (Html.BeginForm()) { 
    @Html.ValidationSummary(true) 

    <fieldset> 
     <legend>Test</legend> 

     <div class="editor-label"> 
      @Html.LabelFor(model => model.StartDate) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.StartDate) 
      @Html.ValidationMessageFor(model => model.StartDate) 
     </div> 

     <div class="editor-label"> 
      @Html.LabelFor(model => model.EndDate) 
     </div> 
     <div class="editor-field"> 
      @Html.EditorFor(model => model.EndDate) 
      @Html.ValidationMessageFor(model => model.EndDate) 
     </div> 

     <p> 
      <input type="submit" value="Save" /> 
     </p> 
    </fieldset> 
} 

<div> 
    @Html.ActionLink("Back to List", "Index") 
</div> 

@section Scripts { 
    @Scripts.Render("~/bundles/jqueryval") 
} 

共享/ EditorTemplates/DateTime.Cshtml

@model DateTime? 
@{ 
    string editText = Model.HasValue ? Model.Value.ToShortDateString() : ""; 
} 
@Html.TextBox("", editText, new { @class = "datepicker" }) 

<script type="text/javascript"> 
    $(function() { 
     $(".datepicker").datepicker({ 
      dateFormat: "yy/mm/dd" 
     }); 
    }); 
</script> 

以下是CSS和JS文件的順序。

@Styles.Render("~/Content/css") 
    @Styles.Render("~/Content/themes/base/jquery-ui.css") 
    @Scripts.Render("~/bundles/jquery") 
    @Scripts.Render("~/Scripts/jquery-migrate-1.1.1.js") 
    @Scripts.Render("~/bundles/jqueryui") 
    @Scripts.Render("~/bundles/modernizr") 

這似乎是別的什麼導致的問題,而不是任何腳本文件或其他任何東西。您可能想在新項目中嘗試我的代碼。