7

I have this controller action :

[HttpGet]
public ActionResult CreateForm(Models.ReminderModel model)
{
    if (model.FkUserId == null && model.FkRoleId == null) {
        model.FkUserId = UserSession.Current.UserId;
        model.ReminderDate = DateTime.UtcNow;
    }

    return View(model);
}

The action behaves normally if no URL param is specified, but when I specify some data, everything gets set BUT ReminderDate. For example, the request is performed with

model[0][name]:FkUserId
model[0][value]:2
....
model[2][name]:ReminderDate
model[2][value]:2013-03-09T10:33:04.934Z
...

Note : the params are serialized view jQuery and always worked fine until now. It's the first time we try to pass a DateTime back to a controller action.

In the action controller, model.FKUserId will correctly be set, however ReminderDate will be set to "0001-01-01T00:00:00.000Z".

What am I missing?

** Update **

As it turned out, C# will not work with ISO formatted date time strings. It prefers UTC ones. Something like 2013-03-05T16:23:00.000Z needs to be sent as u20130305212358000 (EST). A binding would've been sufficient as well, but I think this is the best solution.

Yanick Rochon
  • 51,409
  • 25
  • 133
  • 214
  • Can you step into the code? Is model.ReminderDate the correct value before you send it back to the View? – TTT Mar 05 '13 at 15:57
  • no, the controller action is invoked with `model.ReminderDate = "0001-01-01T00:00:00.000Z"` (right from the start) even if the URL param is explicitly set, just like all the other arguments... that are correctly set into the model. It's like if the URL param is just ignored. – Yanick Rochon Mar 05 '13 at 16:00
  • 1
    My bet is a datetime format issue. Does this help: http://stackoverflow.com/questions/6076961/pass-date-values-from-ajax-call-to-mvc – TTT Mar 05 '13 at 16:16
  • Could you show the exact query string you used to invoke this controller action? In your question you have shown parameters as if you were POSTing to this controller action. But your controller action is decorated with the `[HttpGet]` attribute meaning that it can only be invoked with GET verb. – Darin Dimitrov Mar 05 '13 at 16:18
  • @Darin I'm guessing this is a PRG. Oh- and if it is, yanick,- can you step into the values sent in from the POST action? – TTT Mar 05 '13 at 16:25
  • @YanickRochon Show ReminderModel model code – Yair Nevet Mar 05 '13 at 16:30
  • 1
    @TTT, so where's the model binding problem then (because this is a model binding problem)? In the Post or in the Get action? If it's in the Post action then why did the OP show his Get action and if it's in the Get action I would like to see the exact query string with which this Get action was called. – Darin Dimitrov Mar 05 '13 at 16:47
  • @DarinDimitrov. I agree, I thought it strange to see the Model passed into a Get, along with what looked like Post params, which is why I thought PRG. But if PRG, I doubt the problem is between the POST and the GET. It's either in the Query string to the GET, or if PRG a formatting issue in the View. – TTT Mar 05 '13 at 17:16
  • GET or POST, it does not make a difference. And the data is pasted from Chrome developper tool; either GET or POST, the data looks the same with either method in that debugger. In any case, I tested both method, and it seems to be that C# cannot parse that date time format. ...I'm not sure what exact format it expects so it can successfully be set into the model. – Yanick Rochon Mar 05 '13 at 19:34

1 Answers1

3

You would get that date of 1/1/0001 because when MVC can't find a value to supply, or can't apply the available value for some reason, the date's default remains, which for DateTime is 1/1/0001 or DateTime.MinValue. If you used DateTime?, you would see null.

It would be good to use Fiddler or Firebug to inspect the request and see what the value being sent is. It most likely may need to be adjusted. You could also check the current form's response via the Form collection and manually convert the value yourself too.

Some alternatives is MVC model binding (though some mention cons to this approach). Some more info is listed below:

Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
Brian Mains
  • 50,520
  • 35
  • 148
  • 257
  • The execution works just fine. I've got to know that there's a problem with that date format, where C# cannot parse it correctly (which is strange since it's a value generated by C#). Apparantly, I need to convert the string into a different format for the controller to be able to convert it back.... I'm actually working on a solution, but would appreciate to have something cleaner :) – Yanick Rochon Mar 05 '13 at 19:28
  • Updated my post above. One person made a custom model binder to handle some date situations and localization. You could consider this as well, as the model binder attempts to take user input and bind it to your property. – Brian Mains Mar 05 '13 at 19:40
  • I'm closing this now. Thanks for the support! – Yanick Rochon Mar 05 '13 at 21:28