【问题标题】:ASP.NET MVC 3: Integrating with the jQuery UI date pickerASP.NET MVC 3:与 jQuery UI 日期选择器集成
【发布时间】:2011-08-09 18:56:33
【问题描述】:

我正在使用 Stuart Leeks 的本指南:ASP.NET MVC 3: Integrating with the jQuery UI date picker and adding a jQuery validate date range validator

我得到这个“奇怪”的错误:“传入字典的模型项是空的,但是这个字典需要一个非空的‘System.DateTime’类型的模型项。” ...在我的 create.cshtml 视图中。

我使用了一些不同的方法,因为我(目前)只对让实际插件工作感兴趣。而且我还希望能够在创建项目时使用日期选择器。

我的“Speaker.cs”模型:

[DataType(DataType.Date)]
[DefaultValue("00-00-00")]   <- thought this might fix the "not null error", with no succes..
public DateTime ReleaseDate { get; set;  }

在“views/Speaker”EditorTemplates 文件夹下添加了部分视图“Date.cshtml”。

@model DateTime
@Html.TextBox("", Model.ToString("dd/MM/yyyy"), new { @class = "date" })

我的 create.cshtml 视图

@model HiFiWarehouse.Models.Speaker
@Html.EditorFor(model => model.ReleaseDate)

在脚本文件夹中添加了“EditorHookup.js”

/// <reference path="jquery-1.5.1.js" />
/// <reference path="jquery-ui-1.8.11.js" />

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

在控制器中创建动作

//
// GET: /Speaker/Create
[Authorize(Roles = "Administrators") 
public ActionResult Create()
{
    return View();
} 

//
// POST: /Speaker/Create

[HttpPost]
[Authorize(Roles = "Administrators")]
public ActionResult Create(Speaker speaker)
{
    if (ModelState.IsValid)
    {
        db.Speakers.Add(speaker);
        db.SaveChanges();
        return RedirectToAction("Index");  
    }

    return View(speaker);
}

我的 _Layout.cshtml 标头:

<head>
    <meta charset="utf-8" />
    <title>@ViewBag.Title</title>
    <link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
    <link href="@Url.Content("~/Content/themes/base/jquery-ui.css")" rel="stylesheet" type="text/css" />

    <script src="@Url.Content("~/Scripts/jquery-1.5.1.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery-ui-1.8.11.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/EditorHookup.js")" type="text/javascript"></script>

    <script src="@Url.Content("~/Scripts/modernizr-1.7.min.js")" type="text/javascript"></script>
</head>

我在这里做错了什么? :)

【问题讨论】:

    标签: asp.net-mvc-3 jquery-ui razor


    【解决方案1】:

    它告诉您需要将 ReleaseDate 设为可为空的类型:

    public DateTime? ReleaseDate { get; set;  }
    

    另一个问题是 00-00-00 的 DefaultValue 不起作用。据我所知 DateTime.MinValue 是 0001-01-01。

    【讨论】:

    • 抱歉没有解决问题:(
    • 已修复!你几乎是正确的(我误解了)。问题是我的部分观点不接受空日期。 @model 日期时间?
    • 是的。我没想到 :) 干得好 :)
    猜你喜欢
    • 2010-09-27
    • 1970-01-01
    • 2010-10-28
    • 2018-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多