0

I am working on MVC and Jquery, and I had a form to create a new event for the website. On the date part, I use datepicker to show the calendar and lets user the select a date. my format is:

 format: 'dd/mm/yyyy',

but when I submit the form, it always failed and told me invalid format. I found the problem was when I selected the date March 24, 2016, it set month was 24, and date was 3(means March).

what's going on here, can anyone help me? Thank you very much.

error messages

how the dates are formatted

Model:

 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; }
}
ccy
  • 341
  • 6
  • 18
  • the problem is that your `start date` and `end date` are taking dates as the format 'MM/dd/yyyy' and there isn't a month that is 23 or 24... can you post your model? – Grizzly Mar 23 '16 at 19:04

1 Answers1

0

In your class, over top of the start date and end date properties add this:

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; }
Grizzly
  • 5,873
  • 8
  • 56
  • 109
  • Hi, thanks for your reply, and I post my code. DataFormatString and ApplyFormatInEditMode are right for my code? – ccy Mar 23 '16 at 19:15
  • @kcc I saw that you posted your code, thank you. And they should be.. you already have data annotations in your model so they should work.. i will edit to show you how it should look based off of what you posted – Grizzly Mar 23 '16 at 19:16
  • should I exchange the date format which you provide to {0:dd/MM/yyyy} – ccy Mar 23 '16 at 19:33
  • @kcc that is your preference – Grizzly Mar 23 '16 at 19:33
  • I tried, its not working. May be I add the code on wrong place – ccy Mar 23 '16 at 19:44
  • @kcc still same error? if so, try with `MM/dd/yyyy`.. also did you get rid of `format: 'dd/mm/yyyy'`? – Grizzly Mar 23 '16 at 19:45
  • yes, still error, my format is dd/mm/yyyy – ccy Mar 23 '16 at 19:53
  • @kcc okay so change the format to my original post was just to see if it works – Grizzly Mar 23 '16 at 19:54
  • I think the format and the startdate should be as same format, right, and I keep both is dd/MM/yyyy, but still errors. – ccy Mar 23 '16 at 19:54
  • @kcc ok so change the format to what I posted to see if that works – Grizzly Mar 23 '16 at 19:56