2011-12-18 10 views
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-上のフォルダをDateTimeのパーシャルビューを追加します。

/// <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-そして_layoutに必要なスクリプトとCSS参照を追加します。

@model System.DateTime 
@Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue 
, new { data_datepicker = true }) 

3 - が_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> 

この実装では、編集ビューはデータピッカーで正しく機能しましたが、 wを実行して、ビューを作成するとどうなりますか?問題はどこだ?

答えて

4
@model System.DateTime 
@Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue 
, new { data_datepicker = true }) 

@model System.DateTime? 
@Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue 
, new { data_datepicker = true }) 

は空のテキストボックスの値

を可能にしなければなりません
関連する問題