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?

2
  • Have you looked at the Request itself? Something like firebug should help. Then look at the POST value for a normal post vs the jquery post? Is the value formatted any differently? Commented Jun 10, 2010 at 22:02
  • Checked with fiddler, values are identical. Commented Jun 10, 2010 at 23:51

3 Answers 3

0

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

Sign up to request clarification or add additional context in comments.

1 Comment

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")
0

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

Comments

0

DateTime is value type and not reference type and it takes DateTime.MinValue if you don't initialize it. you have to declare it as:

DateTime? dt;

Or

Nullable<DateTime> dt;

DateTime? is shorthand for Nullable<DateTime>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.