【问题标题】:have the date validation errors with datepicker [duplicate]使用 datepicker 出现日期验证错误 [重复]
【发布时间】:2016-03-23 19:00:38
【问题描述】:

我正在研究 MVC 和 Jquery,并且我有一个表单可以为网站创建一个新事件。在日期部分,我使用 datepicker 来显示日历并让用户选择一个日期。我的格式是:

 format: 'dd/mm/yyyy',

但是当我提交表单时,它总是失败并告诉我格式无效。我发现问题是当我选择 2016 年 3 月 24 日的日期时,它设置的月份是 24,日期是 3(表示三月)。

这是怎么回事,谁能帮帮我?非常感谢。

error messages

how the dates are formatted

型号:

 public SeminarListViewModel()
    {
        Documents = new List<DocumentDisplayViewModel>();
    }

    public string EventID {get;set;}
    public string Name { get; set; }
    public short Capacity { get; set; }

    [DataType(DataType.Date)]
    public DateTime StartDate { get; set; }

    [DataType(DataType.Date)]
    public DateTime EndDate { get; set; }

    public List<DocumentDisplayViewModel> Documents { get; set; }
}

【问题讨论】:

  • 问题是您的start dateend date 将日期作为格式'MM/dd/yyyy' 并且没有23 或24 的月份......你能发布你的模型?

标签: c# jquery asp.net-mvc


【解决方案1】:

在您的课程中,在 start dateend date 属性之上添加以下内容:

using System.ComponentModel.DataAnnotations; /*put above namespace*/

[Display(Name = "Start Date:")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)]
public System.DateTime StartDate { get; set; }

[Display(Name = "End Date:")]
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)]
public System.DateTime EndDate { get; set; }

【讨论】:

  • 您好,感谢您的回复,我发布了我的代码。 DataFormatString 和 ApplyFormatInEditMode 是否适合我的代码?
  • @kcc 我看到你发布了你的代码,谢谢。它们应该是..您的模型中已经有数据注释,因此它们应该可以工作..我将根据您发布的内容进行编辑以向您展示它的外观
  • 我应该将您提供的日期格式交换给 {0:dd/MM/yyyy}
  • @kcc 这是你的喜好
  • 我试过了,它不起作用。可能是我在错误的地方添加了代码
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-18
  • 2012-10-14
  • 2018-07-06
相关资源
最近更新 更多