Nullable DateTime Parameter is never bound when calling the action
I have the following function signuture:
public JsonResult PopulateGrid(int page, Guid? accountId, Guid? systemUserId, Guid? branchId, DateTime? fromDate, DateTime? toDate, HomeVisitType? homeVisitType)
Every single parameter is bound just fine except toDate
which turns out to be always null.
Request.QueryString["toDate"]
it retrives the right value which is 30/09/2010.
It seems that DateTime expects开发者_开发技巧 another format when binding.
What is the right format?A quick test on my system shows that it expects the data in MM/DD/YYYY and not DD/MM/YYYY which is probably why you're having problems. My guess is that if you try the same date on the fromDate you'll also have the same null issue.
I've changed the current culture in my app to one that uses the DD/MM/YYYY and it seemed to have no effect. Seems to have the same problem with the , decimal for a language that uses 10,01 instead of 10.01...
Update from someone a developer on the ASP.Net team.
"This is intentional. Anything that is part of the URI (note the 'Uniform' in URI) is interpreted as if it were coming from the invariant culture. This is so that a user in the U.S. who copies a link and sends it over IM to a friend in the U.K. can be confident that his friend will see the exact same page (as opposed to an HTTP 500 due to a DateTime conversion error, for example). In general, dates passed in RouteData or QueryString should be in the format yyyy-mm-dd so as to be unambiguous across cultures.
If you need to interpret a QueryString or RouteData parameter in a culture-aware manner, pull it in as a string, then convert it to the desired type manually, passing in the desired culture. (DateTime.Parse has overloads that allow you to specify a culture.) If you do this, I recommend also taking the desired culture as a QueryString or a RouteData parameter so that the 'Uniform' part of URI isn't lost, e.g. the URL will look something like ...?culture=fr-fr&date=01-10-1990."
精彩评论