2012-07-10 163 views
0

视图内部 - >共享文件夹我有DateTime.ascx局部视图;MVC2:Datepicker不能在编辑器模板中工作

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<System.DateTime?>" %> 
<%:Html.TextBox("", (Model.HasValue ? Model.Value.ToLongDateString() : string.Empty), 
    new { @class = "datePicker" })%> 

这在大多数我的应用程序得益于jQuery库http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.js和这段JavaScript代码的;

 // noWeekendsOrHolidays 
     // beforeShowDay: $.datepicker.noWeekends 
     $(function() { 
      $(".datePicker").datepicker({ showOn: 'both', dateFormat: 'dd MM yy', changeMonth: true, changeYear: true, yearRange: 'c-1:c+1', beforeShowDay: noWeekendsOrHolidays }); 
     }); 

我有一个观点如下;

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.master" 
Inherits="System.Web.Mvc.ViewPage<SHP.WebUI.Models.BankHolidayViewModel>" %> 

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"> 
    BankHoliday 
</asp:Content> 

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> 
    <% using (Html.BeginForm("Create", "BankHoliday")) 
     {%> 
     <%: Html.AntiForgeryToken() %> 
     <h3>Bank Holiday Administration</h3> 
     <% if (TempData["UpdatedFlag"].ToString() == "True") 
     { %> 
     <p class="success">At time <% Response.Write(DateTime.Now.ToString("T")); %> - Details have been successfully saved 
      </p> 
     <%} 
     else if (TempData["UpdatedFlag"].ToString() == "False") 
     {%> 
     <p class="error">At time <% Response.Write(DateTime.Now.ToString("T")); %> - ERROR! Invalid date entered has NOT been saved 
      </p> 
     <%} %>   
      <p>Select the year: <%: Html.DropDownListFor(model => model.SelectedYear, Model.YearList)%></p> 
      <fieldset> 
      <legend>Enter the bank holidays here:</legend> 
      <p><i>You can find the bank holiday dates on this <a target="_blank" href="http://www.year-planner-calendar.wanadoo.co.uk/">website</a>.</i> </p>  
      <table class="groupBorder"> 
       <tr> 
        <th>Bank Holiday</th> 
        <th>Date</th> 
        <th>Notes</th> 
       </tr> 
       <%: Html.EditorFor(x => x.BankHolidays) %> 
       <tr> 
        <td colspan="3" style="padding-top:20px;text-align: center"> 
         <input type="submit" value="Save" id="btnSubmit"/> 
        </td> 
       </tr> 
      </table> 
      </fieldset> 
     <% } %> 
     <script language="javascript" type="text/javascript"> 
      $(function() { 
       $("#SelectedYear").change(function() { 
        var year = $("#SelectedYear").val(); 
        $("#wholepage").load('<%:Url.Content(@"~/BankHoliday/Create/")%>' + year); 
       }); 
      }); 
    </script> 
</asp:Content> 

在这里可以看到行:<%:Html.EditorFor(X => x.BankHolidays)%> 这具有相关联的编辑器模板;

“%>
<tr> 
    <td><%: Model.T.BankHolidayDescription%> 
     <%: Html.HiddenFor(model => model.BH.BankHolidayId) %> 
     <%: Html.HiddenFor(model => model.T.BankHolidayTypeId) %> 
    </td> 
    <td><%: Html.EditorFor(model => model.BH.NullableBankHolidayDate)%></td> 
    <td><%: Html.EditorFor(model => model.BH.BankHolidayComment)%></td> 
</tr> 

日期选择器必须适用于<%。Html.EditorFor(型号=> model.BH.NullableBankHolidayDate)%>

但是它不工作我点击现场并没有任何反应

当我按下F12键,进入开发工具,我找到用的DatePicker控件的HTML样子。

<input name="BankHolidays[6].BH.NullableBankHolidayDate" class="datePicker hasDatepicker" id="BankHolidays_6__BH_NullableBankHolidayDate" type="text" jQuery1341928330742="131"/> 

所以这个类已被分配到datePicker,所以肯定它应该工作?

+1

你在谈论http://jqueryui.com/demos/datepicker/? – LeftyX 2012-07-10 14:04:30

+0

另外,您的选择器如何定义应用datepicker?你使用'$('。datePicker')'? – Tejs 2012-07-10 14:08:02

+0

是的,这是正确的 – arame3333 2012-07-10 14:08:42

回答

0

问题是我从Ajax调用填充网格,$(document).ready(function(){})事件没有触发。 当我更改代码以提交POST操作并从中填充网格时,它工作正常。 Ajax调用后,我已经在成功函数中指定了datepicker,但是我无法让它工作。

0

您是否定义了javascript函数noWeekendsOrHolidays
如果你想知道更多,你可以阅读这article

另一件事你可以尝试改变是母版脚本:

<script type="text/javascript"> 
    $(document).ready(function() { 
     $(".datePicker").datepicker(); 
    }); 
</script> 

,并把它放在这个根据雅虎的页面底部document

+0

是,noWeekendsOrHolidays已定义并可在其他地方使用。我不想在我的问题中提供额外的信息。 – arame3333 2012-07-11 15:04:32

+0

@ arame3333:你确定你没有任何JavaScript错误? – LeftyX 2012-07-11 15:30:07

+0

我对此很有信心,因为这适用于其他应用程序。这里的区别在于它在网格中。 – arame3333 2012-07-12 07:41:48