Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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'.
like image 980
mrhassan Avatar asked Apr 29 '12 11:04

mrhassan


1 Answers

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>
like image 89
mrhassan Avatar answered Nov 07 '22 08:11

mrhassan