【发布时间】: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