Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Datetime picker MVC3

Tags:

asp.net-mvc

I have in my Model this field

[DataType(DataType.DateTime)]
    [Required(ErrorMessage = "Expire is required")]
    public DateTime Expire
    {
      get;
      set;
    }

In My View

@Html.EditorFor(model => model.Expire))
      @Html.ValidationMessageFor(model => model.Expire)

and I create DataTime EditorTemplates

@inherits System.Web.Mvc.WebViewPage<System.DateTime>
@Html.TextBox("", (Model.ToShortDateString()), new { @class = "datePicker" })
<script type='text/javascript'>
  $(document).ready(function () {
    $(".datePicker").datepicker({
      //      buttonImage: "/content/images/calendar.gif",
      //      showOn: "both",
      //      defaultDate: $("#calendar-inline").attr('rel')
      showAnim: 'slideDown',
      dateFormat: 'dd/mm/yyyy'

    });
  });
</script>

when i try to Create new Item I have this error message

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

I try

Model.HasValue ? Model.Value.ToString("MM/dd/yyyy") : DateTime.Today.ToShortDateString()

But i not have in Model Value check

like image 384
amchoni Avatar asked Jan 06 '11 16:01

amchoni


1 Answers

Try one of the following:

  • If the action is for creating a new object pass a new instance as model, e.g. return View(new MyObject())
  • Change @inherits System.Web.Mvc.WebViewPage<System.DateTime> to @inherits System.Web.Mvc.WebViewPage<System.DateTime?>
like image 120
Max Toro Avatar answered Sep 22 '22 12:09

Max Toro