【问题标题】:Why datepicker doesn't display in my MVC 3 Website?为什么 datepicker 不显示在我的 MVC 3 网站中?
【发布时间】:2012-03-25 22:17:21
【问题描述】:

我正在关注这个tutorial,以便在我点击文本框时显示日期选择器...但是日期选择器不显示...

模型类(预留):

    [Required(ErrorMessage = "Date requise.")]
    [Display(Name = "Date")]
    [Column("Date")]
    [DataType(DataType.DateTime)]
    public DateTime Date { get; set; }

EditorHookup.js

/// <reference path="~/Scripts/jquery-1.5.1.js" />
/// <reference path="~/Scripts/jquery-ui-1.8.11.js" />
$(document).ready(function () {
$('.date').datepicker({ dateFormat: "dd/mm/yy" });
});

部分视图(Date.cshtml)

@inherits System.Web.Mvc.WebViewPage<System.DateTime?>
@model DateTime
@Html.TextBox("", Model.ToString("dd/MM/yyyy"),
              new{@class="date"})
** TODO Wire up the date picker! **

我的看法

@model TennisOnline.Models.Reservation

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

<h2>Create</h2>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"> </script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery-ui-1.8.11.js")" type="text/javascript"></script>
<link href="@Url.Content("~/Content/themes/base/jquery.ui.all.css")" rel="stylesheet" type="text/css" />
<script src="@Url.Content("~/Scripts/EditorHookup.js")" type="text/javascript"></script>

@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
    <legend>Reservation</legend>

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

那么,你知道为什么日期选择器不显示吗?提前致谢

【问题讨论】:

    标签: c# asp.net-mvc-3 razor datepicker


    【解决方案1】:

    未调用您的局部视图模型,因为您的属性的 DataType 错误。替换

    [DataType(DataType.DateTime)]
    

    [DataType(DataType.Date)]
    

    一切都应该按预期工作。

    编辑

    您还应该从局部视图中删除 @inheritsline,因为模型不允许继承。

    【讨论】:

    • 之后,我发现了一个 HttpParseException : The 'inherits' keyword is not allowed when a 'model'关键字被使用。
    • 至少现在你知道你的局部视图被调用了。你在进步!您应该摆脱 @inherits 行。我正在更新我的答案。
    • 谢谢我删除了@inherit 行...但是我现在有一个新的异常抱歉:( ==> InvalidOperationException : 传入字典的模型项为空,但是这个字典需要一个非-“System.DateTime”类型的空模型项。
    • 你应该从你的控制器返回类似 return View(new Reservation { Date = DateTime.Now });
    • 您可以通过让您的模型成为日期时间来允许空值吗? (可为空)而不是日期时间
    【解决方案2】:

    我认为您的视图缺少对 ="~/Scripts/jquery-1.5.1.js" 脚本的引用。

    【讨论】:

    • 谢谢,我添加了参考,但我的问题仍然存在:(
    【解决方案3】:

    我认为您的局部视图可能需要命名为 DateTime.cshtml 以匹配您的模型属性的类型。

    【讨论】:

      猜你喜欢
      • 2013-03-05
      • 1970-01-01
      • 2011-02-18
      • 1970-01-01
      • 2023-03-26
      • 1970-01-01
      • 2012-11-22
      • 1970-01-01
      相关资源
      最近更新 更多