【问题标题】:jQuery Datetime picker MVC3jQuery 日期时间选择器 MVC3
【发布时间】:2011-01-06 16:53:00
【问题描述】:

我的模型中有这个字段

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

在我看来

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

我创建了 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>

当我尝试创建新项目时出现此错误消息

传入的模型项 字典为空,但这 字典需要一个非空模型 'System.DateTime' 类型的项目

我试试

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

但我没有在模型值检查中

【问题讨论】:

    标签: asp.net-mvc


    【解决方案1】:

    尝试以下方法之一:

    • 如果操作是为了创建一个新对象,则传递一个新实例作为模型,例如return View(new MyObject())
    • @inherits System.Web.Mvc.WebViewPage&lt;System.DateTime&gt; 更改为@inherits System.Web.Mvc.WebViewPage&lt;System.DateTime?&gt;

    【讨论】:

      【解决方案2】:
      @inherits System.Web.Mvc.WebViewPage<System.DateTime> 
      

      应该是

      @inherits System.Web.Mvc.WebViewPage<System.DateTime?>
      

      【讨论】:

        【解决方案3】:

        只是放?在你的 System.DateTime

        例子:

        @model System.DateTime?
        
        @Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue,
                        new
                        {
                            data_datepicker = true
                        })
        

        如果你省略问号,你会报错

        【讨论】:

          【解决方案4】:

          这是我的编辑器模板的代码:重点是避免空值:

          @model DateTime?
          
          
          <script src="../../../Scripts/jquery-1.4.4.js" type="text/javascript"></script>
          <script src="../../../Scripts/jquery-ui.js" type="text/javascript"></script>
          
                     @Html.TextBox("", (Model.HasValue ? Model.Value.ToShortDateString() : String.Empty), 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>
          

          【讨论】:

            猜你喜欢
            • 2011-12-02
            • 2012-09-11
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-03-27
            • 2015-08-04
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多