1

I'm trying to send via POST or PUT a simple DateTime property in UNIX time format:

{ ... "Created": "/Date(1307871513107+0700)/" ... } 

to my ASP.NET MVC 4 WEBAPI action:

...
// PUT /api/<controller>/<action>
public HttpResponseMessage PutUser(UserInfo userInfo)
{
...

UserInfo is a usual model

...
public DateTime Created { get; set; }
...

which is used for EF4 DbContext

...
public class DgDbContext : DbContext
  {
public DbSet<UserInfo> UserInfo { get; set; }
...

In debugging and getting my model in controller's action, I have my DateTime prop, but in wrong format:

{01.01.0001 0:00:00}

Where is my mistake and how I can fix it?

All the other fields are getting correct. Strings like "dd.MM.yyyy hh:mm:ss" are working good. I've tried to send:

  • { "Created": "Date(1307871513107)" }
  • { "Created": "/Date(1307871513107)/" }
  • { "Created": "/Date(1307871513107)/" }
  • { "Created": "\Date(1307871513107)\" }
  • etc.

Thanks.

Sergey Shabanov
  • 176
  • 2
  • 11

1 Answers1

3

Basically you have three options:

  • Receive the data as a ViewModel with the date as string and then parse it like this
  • Define two properties on your model, one ignored for the EF (as string) and the other will be empty from AJAX (as date). Parse the value after you receive the date in your controller
  • Create a custom model binder

Choose one that is easier for you which I suppose is the second one.

Community
  • 1
  • 1
Aliostad
  • 80,612
  • 21
  • 160
  • 208