2

I'm using MVC 4 and I have a form field that accepts two dates. One for checkin and the other for checkout. The class is very basic:

public class date {
     public DateTime Checkin { get; set; }
     public DateTime Checkout { get; set; }
}

The format I'm using is mm/dd/yyyy where the user inputs the checkin date using a javascript calendar and adjusts the checkout date with the jquery mobile slider. However, I seem to always get "01/01/0001 12:00:00 AM" instead of the actual date. I gave the solution to my co-workers and they do not have the problem even though its the exact same code.

Is there a setting that I can change or is my visual studio messed up?

J-Y
  • 365
  • 3
  • 9
  • 18

3 Answers3

2

Do you have the code where those properties are being assigned? That's the default value for DateTime, so whatever is supposed to be assigning it is probably not executing at all. Is date a model? If you're relying on the mvc model binder, make sure your controller action accepts a parameter of type date. (and maybe think about improving the name also)

recursive
  • 83,943
  • 34
  • 151
  • 241
1

The class you have shown initializes Checkin and Checkout to DateTime.MinValue, which is 12am on 01/01/0001.

You do not show any code that changes that value from the initialized value.

Presumably the binding from the UI to the model is incorrect. You would have to post more code to diagnose what is wrong exactly. Suggest you post the action in the controller where you accept new values from the UI.

For guidance on what can go wrong using a DateTime as a parameter to a controller action, see this similar post:

How do I pass a datetime value as a URI parameter in asp.net mvc?

Community
  • 1
  • 1
Eric J.
  • 147,927
  • 63
  • 340
  • 553
0

Your binding on the client side is not working, please take a look at the names of the input that you are using for the dates, for example, check below, you can have the id for your datepicker but the attribute name should have this values: Checkin and the other Checkout

<input id=datepicker name="Checkin" />
<input id=datepicker name="Checkout" />
Zinov
  • 3,817
  • 5
  • 36
  • 70