Share via


DateTime turns into 01/01/0001

Question

Wednesday, August 31, 2011 10:06 PM

I have an date update field  which works fine after I enter a date manually in below statement:

<div class="editor-field">Html.TextBoxFor(model => model.upDateTime) Html.ValidationMessageFor(model => model.upDateTime> </div>

However, if I change the above line to read like line below so I can get date automatically, the date being passed to create statement is incorrect (01/01/0001).  This later causes the datetime2 to datetime problem exception.

<div class="editor-field">Html.TextBox("date", DateTime.Now.ToString()) Html.ValidationMessageFor(model => model.upDateTime> </div>

It does the same with a Html.Label field.  Does anyone know what could be causing this?

 

All replies (8)

Thursday, September 1, 2011 12:52 AM ✅Answered

Html.TextBoxFor(model => model.upDateTime)

Instead of changing to

Html.TextBox("date", DateTime.Now.ToString())

please change to

Html.TextBox("upDateTime", DateTime.Now.ToString())

like your property name.


Thursday, September 1, 2011 1:11 AM ✅Answered

  Didn't know that that name had to be indentical

Otherwise MVC does not fill the property from HTML...

hanks,  any chance this can be made to work with a @Html.Label.

IF you put a hidden.... JHOwever, I suggest you fill the Datatime from the controller action


Wednesday, August 31, 2011 10:12 PM

model.upDateTime is not nullable, so the default value is min date (which is too low and invalid for sqlserver) . Make it nullable (DateTime?) also you may want to set a custom format if you don't want the time included)


Thursday, September 1, 2011 12:09 AM

Try to set the format for the date as "MM/dd/yyyy", while in automatic mode.

<div class="editor-field">Html.TextBox("date", DateTime.Now.ToString("MM/dd/yyyy")) Html.ValidationMessageFor(model => model.upDateTime> </div>

Try below code if you need to include time also

<div class="editor-field">Html.TextBox("date", DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss")) Html.ValidationMessageFor(model => model.upDateTime> </div>

Thursday, September 1, 2011 12:56 AM

I tried the solutions above to no avail, however, after making the datetime nullable made me realize that no date is being passed,, therefore the min date. While nullabe, the date was blank when it got to the create post method which is no good.  So the question is why the date is not being passed, except when I use the TextBoxFor. ????


Thursday, September 1, 2011 1:00 AM

Have you tried ignatandrei's solution?

Looks like you are passing the wrong property name to the Textbox helper method.

If it still gives trouble after passing the right property, then try formatting the date.


Thursday, September 1, 2011 1:05 AM

Inatandrei:

 

I tried your solution and it works.  Didn't know that that name had to be indentical.  Thanks,  any chance this can be made to work with a @Html.Label.

My preference would have been a label, but I'm willing to settle for a textbox.


Thursday, September 1, 2011 12:48 PM

By the way I did as you suggested and updated my datetime field in the Controller action.  It is much more flexible to work from there.

 

Thanks,