错误:传入字典的模型项为null,但此字典需要"System.DateTime"类型的非null模型项

mrh*_*san 6 c# asp.net-mvc-4

在我的模型中

[DataType(DataType.Date)]
    [Display(Name="Event Date")]
    [DisplayFormat(DataFormatString = "{0:d}")]
    //[DisplayFormat(ApplyFormatInEditMode=true ,DataFormatString="{0:DD/MM/YYYY}")]
    public DateTime EventDate { get; set; }
Run Code Online (Sandbox Code Playgroud)

在我的视图中(Create.cshtml)

<div class="editor-label">
        @Html.LabelFor(model => model.EventDate)
    </div>
    <div class="editor-field">
        @Html.EditorFor(model => model.EventDate)
        @Html.ValidationMessageFor(model => model.EventDate)
    </div>
Run Code Online (Sandbox Code Playgroud)

在Shared/EditorTemplates/Date.cshtml中

@model DateTime
@Html.TextBox("", String.Format("{0:d}", Model.ToShortDateString()),
  new { @class = "datefield", type = "date" })
Run Code Online (Sandbox Code Playgroud)

并得到以下错误

The model item passed into the dictionary is null, but this dictionary requires a non-null model item of type 'System.DateTime'.
Run Code Online (Sandbox Code Playgroud)

mrh*_*san 6

谢谢解决它....只是把脚本放在文件中..

@model Nullable<DateTime>



@{
     DateTime dt = DateTime.Now;
 if (Model != null)
 {
     dt = (System.DateTime)Model;

 }
@Html.TextBox("", String.Format("{0:d}", dt.ToShortDateString()), new { @class = "datefield", type = "date" })
} 

<script type='text/javascript'>
    $(document).ready(function () {
        $(".datefield").datepicker({
            //      buttonImage: "/content/images/calendar.gif",
            //      showOn: "both",
            //      defaultDate: $("#calendar-inline").attr('rel')

            showAnim: 'slideDown',
            dateFormat: 'dd/mm/yy'

        });
    });
</script>
Run Code Online (Sandbox Code Playgroud)