【问题标题】:Issue in DateTime Picker MVC2日期时间选择器 MVC2 中的问题
【发布时间】:2010-11-17 07:47:55
【问题描述】:

我使用 DateTime Picker 完成了一个应用程序。当前日期显示在控件中。但是日历不工作。每当我点击文本框时,什么都没有发生..

这是我在 Site.Master 中的代码

 <title>
    <asp:ContentPlaceHolder ID="TitleContent" runat="server" />
</title>
<link href="../../Content/Site.css" rel="stylesheet" type="text/css" />
<link href="../../Content/jquery-ui-1.8.1.custom.css" rel="stylesheet" type="text/css" />
<script src="../../Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<script src="../../Scripts/jquery-ui-1.8.1.custom.min.js" type="text/javascript"></script>

<script type="text/javascript">
    $(document).ready(function () {
        $("#UseDatePicker").live('click', function () {
            $(this).datepicker('destroy').datepicker({
                showOn: 'focus',
                changeMonth: true,
                changeYear: true
            }).focus();
        });
    });
</script>

在我的 index.aspx...

<p><%:Html.LabelFor(m => m.MyDate)%>:&nbsp;<%:Html.EditorFor(m => m.MyDate) %></p>

在我的 Model.cs 中

 [DisplayName("Date")]
    public DateTime MyDate { get; set; }

在我的 DateTime.ascx 中

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<System.DateTime?>" %>
<%=Html.TextBox("", (Model.HasValue ? Model.Value.ToString("MM/dd/yyyy") : DateTime.Today.ToShortDateString()), new { @class = "UseDatePicker date-text-box" })%>

【问题讨论】:

    标签: jquery asp.net-mvc asp.net-mvc-2 datepicker


    【解决方案1】:

    在 Site.Master 的 javascript 中,您有一个看起来不正确的选择器 - 您有 #UseDatePicker - 这是一个 ID 选择器,但在您的编辑器模板中,您分配了 类 UseDatePicker.

    您可能希望将站点主控中的选择器更改为 .UseDatePicker

    而且我不知道你为什么要做整个破坏的事情 - 我不知道你想要在那里实现什么......

    【讨论】:

      【解决方案2】:

      其实应该是:

      $(document).ready(function () {
          $('.UseDatePicker').datepicker({
              showOn: 'focus',
              changeMonth: true,
              changeYear: true
          });
      });
      

      【讨论】:

      • 我得到了警报的值..但不是日历........
      • 不知道您的代码有什么问题,但这里有一个演示,说明了一个工作示例:jsfiddle.net/mr92f。确保您已正确包含所有脚本并且 css 类名称正确。
      • 在上面的源码中,我们提到了点击文本框,这样就可以显示日历了
      • 我添加了一个 ViewUserControl1.ascx 文件(即 DateTime.ascx)并给出了这个代码“" %> " ....我是否应该更改其中的任何内容...
      【解决方案3】:

      我得到了警报的价值..但不是日历。

       <script type="text/javascript">
          $(document).ready(function () {
              alert('a');
              $('.UseDatePicker').datepicker({
                  showOn: 'focus',
                  changeMonth: true,
                  changeYear: true
              });
          });
      </script>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-08-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-07
        • 1970-01-01
        • 1970-01-01
        • 2015-04-28
        相关资源
        最近更新 更多