1

When i post a DateTime with Json i have the following error: Error 500 Can't cast object of type "System.DateTime" to type "System.Array".

I don't understant why ! Can you help me please ?

the header send look like this :

{"MyDate":"2012-12-31T23:00:00.000Z","Param1":"aaaa","IdItem":123}

My viewModel :

 public class MyViewModel
 {
public DateTime MyDate { get; set; }
public string Param1 {get;set;}
public Int32? IdItem { get; set; }
 }

My controller :

[HttpPost]
    public void Saisie(MyViewModel model)
    { ... }

My Javascript code :

$.ajax({
        url: url,
        type: 'post',
        dataType: 'json',
        data : JSON.stringify(model),
        contentType: 'application/json',
success : function() {...}
})
j0k
  • 22,600
  • 28
  • 79
  • 90

2 Answers2

2

Use data: $(form).serialize() instead of data : JSON.stringify(model)

Rifaj
  • 1,024
  • 12
  • 21
  • i can't use $(form), i need to post a js variable. – Courtois Olivier Jan 07 '13 at 10:18
  • Try changing the date format before the ajax post. The following link could be helpful.[link]http://stackoverflow.com/questions/2573521/how-do-i-output-an-iso-8601-formatted-string-in-javascript – Rifaj Jan 07 '13 at 12:05
0

I have found my problem, in the view model i have :

public class MyViewModel
{
[MaxLength(10)]
public DateTime MyDate { get; set; }
}

the MaxLength attribut cause the error

  • I just had this problem today, I'd commented a property out, but not the DataAnnotations above it...and there was a DateTime below...drove me crazy for a couple of hours. Thanks for posting your own solution for the greater good. – MongooseNX Jan 18 '14 at 00:15