2
我使用帶有Razor View的ASP.NET MVC3,並嘗試使用Scott Allen's way替換DateTime類型的datepicker。 所以我有一個簡單的模型是這樣的:使用jQuery datepicker和創建視圖無法正常工作
public class Student
{
public long Id { get; set; }
[Required]
public string Name { get; set; }
[DataType(DataType.DateTime)]
public DateTime EnterYear { get; set; }
}
任何改動之前的所有意見工作正常,但是當我添加一些代碼使用datapicker我有一個問題:編輯視圖工作正常,但是當我嘗試去創建視圖有一個錯誤:
The model item passed into the dictionary is null, but this dictionary requires a non-
null model item of type 'System.DateTime'.
Description: An unhandled exception occurred during the execution of the current web
request. Please review the stack trace for more information about the error and where
it originated in the code.
Exception Details: System.InvalidOperationException: The model item passed into the
dictionary is null, but this dictionary requires a non-null model item of type
'System.DateTime'.
Source Error:
Line 59: </div>
Line 60: <div class="editor-field">
Line 61: @Html.EditorFor(model => model.EnterYear)
Line 62: @Html.ValidationMessageFor(model => model.EnterYear)
Line 63: </div>
Source File: d:\Projects\MyProject\Views\Student\Create.cshtml Line: 61
有我的變化:
1- EditorTemplates文件夾添加到共享文件夾視圖中。
2-添加日期時間局部視圖上述文件夾:
@model System.DateTime
@Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue
, new { data_datepicker = true })
3-添加新的腳本_layout視圖:
/// <reference path="../Scripts/jquery-1.5.1-vsdoc.js" />
/// <reference path="../Scripts/jquery-ui-1.8.11.js" />
$(document).ready(function() {
$(":input[data-datepicker]").datepicker();
})
4-和添加需要的腳本和CSS引用_layout:
<link href="@Url.Content("~/Content/themes/base/jquery.ui.all.css")" rel="stylesheet"
type="text/css" />
<script src="@Url.Content("~/Scripts/jquery-ui-1.8.11.min.js")" type="text/javascript">
</script>
在這個實現中,編輯視圖可以正確地與數據選取器一起工作,運行錯誤,創建視圖會發生什麼?哪裏有問題?