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.

9
  • Can you step into the code? Is model.ReminderDate the correct value before you send it back to the View? Commented Mar 5, 2013 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. Commented Mar 5, 2013 at 16:00
  • 1
    My bet is a datetime format issue. Does this help: stackoverflow.com/questions/6076961/… Commented Mar 5, 2013 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. Commented Mar 5, 2013 at 16:18
  • 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. Commented Mar 5, 2013 at 16:47

1 Answer 1

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:

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

3 Comments

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 :)
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.
I'm closing this now. Thanks for the support!

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.