jQuery日期时间选择器MVC3

amc*_*oni 7 asp.net-mvc

我在我的模型中有这个字段

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

在我看来

@Html.EditorFor(model => model.Expire))
      @Html.ValidationMessageFor(model => model.Expire)
Run Code Online (Sandbox Code Playgroud)

我创建了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>
Run Code Online (Sandbox Code Playgroud)

当我尝试创建新项目时,我有此错误消息

传递到字典中的模型项为null,但此字典需要"System.DateTime"类型的非null模型项

我试试

Model.HasValue ? Model.Value.ToString("MM/dd/yyyy") : DateTime.Today.ToShortDateString()
Run Code Online (Sandbox Code Playgroud)

但我没有进行模型价值检查

Max*_*oro 6

请尝试以下方法之一:

  • 如果动作是用于创建新对象,则将新实例作为模型传递,例如 return View(new MyObject())
  • 更改@inherits System.Web.Mvc.WebViewPage<System.DateTime>@inherits System.Web.Mvc.WebViewPage<System.DateTime?>