ERROR : The model item passed into the dictionary is null, but this dictionary requires a non-null model item of type 'System.DateTime'

asp.net-mvc-4, c#

Solution

thanks Solved it with.... just putting the script in the file..

@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>

Problem

In my model ``` [DataType(DataType.Date)] [Display(Name="Event Date")] [DisplayFormat(DataFormatString = "{0:d}")] //[DisplayFormat(ApplyFormatInEditMode=true ,DataFormatString="{0:DD/MM/YYYY}")] public DateTime EventDate { get; set; } ``` in my View (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> ``` in Shared/EditorTemplates/Date.cshtml ``` @model DateTime @Html.TextBox("", String.Format("{0:d}", Model.ToShortDateString()), new { @class = "datefield", type = "date" }) ``` and getting following error ``` The model item passed into the dictionary is null, but this dictionary requires a non-null model item of type 'System.DateTime'. ```

Original source