2012-03-13 45 views
0

我的日期時間選擇器工作正常的「Html.TextBoxFor」,但是當我有「Html.EditorFor」它不顯示anydatepicker。在MVC3.0 datetimepicker

我有一個jQuery如下:

$(document).ready(function() { 
       $('.dp').datepicker({ 
        changeMonth: true, 
        changeYear: true, 
        dateFormat: 'yy-mm-dd' 
       }); 
      }); 

我想在我的.aspx頁面中使用此功能。

<div class="float-left"> 
<%: Html.Label("BAASent Date") %> 
<br /> 
<%:Html.TextBoxFor(model => model.BAASentDate, new { @class = "dp" }) %> 
<%: Html.ValidationMessageFor(model => model.BAASentDate) %> 
</div> 

上面的代碼一切工作正常。 但當我用「Html.EditorFor」替換「Html.TextBoxFor」時,它不顯示任何日期時間選擇器。

這裏怎麼了?

回答

0

當您使用EditorFor你可能不dp類適用於相應的輸入字段等等$('.dp')選擇您正在使用您的deatepicker沒有返回。

一種可能性是該類應用到父DIV:

<div class="float-left dp"> 
    <%= Html.Label("BAASent Date") %> 
    <br /> 
    <%= Html.EditorFor(model => model.BAASentDate) %> 
    <%= Html.ValidationMessageFor(model => model.BAASentDate) %> 
</div> 

,然後調整自己的選擇,讓你獲取輸入字段與給定的類名的DIV中:

$(document).ready(function() { 
    $('.dp :input').datepicker({ 
     changeMonth: true, 
     changeYear: true, 
     dateFormat: 'yy-mm-dd' 
    }); 
}); 

另一種可能性是爲DateTime字段編寫一個自定義編輯器模板,在該字段中將dp類應用於TextBox。所以裏面~/Views/Shared/EditorTemplates/DateTime.ascx

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %> 
<%= Html.TextBox(
    "", 
    ViewData.TemplateInfo.FormattedModelValue, 
    new { @class = "text-box single-line dp" } 
) %> 

然後:

<div class="float-left"> 
    <%= Html.Label("BAASent Date") %> 
    <br /> 
    <%= Html.EditorFor(model => model.BAASentDate) %> 
    <%= Html.ValidationMessageFor(model => model.BAASentDate) %> 
</div> 

最後:

$(document).ready(function() { 
    $('.dp').datepicker({ 
     changeMonth: true, 
     changeYear: true, 
     dateFormat: 'yy-mm-dd' 
    }); 
}); 
+0

你的第二個選項(創建一個自定義模板編輯器)不工作me.Am我缺少什麼? – Santosh 2012-03-13 13:08:29

+0

當我將該類應用於父div時,它對「Html.EditorFor」正常工作。 – Santosh 2012-03-13 13:12:47

+0

我正在將「dp」類應用於「Html.EditorFor」,也與「Html.TextBoxFor」相同。 <%:Html.EditorFor(model => model.BAASentDate,new {@class =「dp」})%>。但這裏的日期時間選擇器不工作,因爲它用於「Html.TextBoxFor」 – Santosh 2012-03-13 13:14:08