views:

68

answers:

2

This is driving me crazy, similar to this. If I use a standard form and no javascript the controller correctly binds the datetime. However when I'm posting from a form it always binds as null:

"MyObject.Name": "Test name",
"MyObject.Date": "5/1/2001"

I've tried a couple of variations, 5-1-2001, etc. but cannot seem to get it to take. I can confirm that it is being passed to the server as it shows up in the Request.Form string. My culture is Gregorian and I've set:

Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture;

In Application_BeginRequest(). What gives?

A: 

Did you try using leading zeros, like "05/01/2001"?

The invariant culture's standard date formats include "MM/dd/yyyy" but not "M/d/yyyy", so if it works, that's why. Ideally you would use one of the non-culture-specific formats like the 'O' roundtrip pattern. Then it wouldn't matter what culture you were using on the server: http://msdn.microsoft.com/en-us/library/az4se3k1.aspx#Roundtrip

InfinitiesLoop
I tried with the leading zeroes. Did not work.I tried "o" roundtrip problem and it did not work.To make sure I'm not going crazy I created a string field to send it to and it correctly sends it ("2001-05-01T00:00:00.0000000") while the DateTime field remains null ("01/01/0001 00:00:00")
chum of chance
A: 

Could it be a localization issue? This blog post describes a common localization pitfall with DateTime.

korchev