AKASH
AKASH

Reputation: 416

Display value of datetime in textboxfor at edit time

I am using HTML5 type = "date" and it's working fine. But when there is type="datetime-local", at the time of editing field it's not displaying using TextBoxFor() in MVC4

This one is working,

@Html.TextBoxFor(c => c.date, "{0:yyyy-MM-dd}", new { @class = "form-control",@placeholder = "DateTime", @type = "date" })

in texbox = "2/15/2015"

but

@Html.TextBoxFor(c => c.datetime, "{0:yyyy-MM-dd HH:mm:ss}", new { @class = "form-control", @placeholder = "OpeningDoor", @type = "datetime-local" })

not displaying data when there is value in "datetime".

textbox = "mm/dd/yyyy _:_:_" instead of "1/15/2015 01:40:00 PM"

Upvotes: 1

Views: 3281

Answers (1)

user3559349
user3559349

Reputation:

The format string needs to be "{0:yyyy-MM-ddTHH:mm:ss}" or you can simply use "{0:s}"

@Html.TextBoxFor(c => c.datetime, "{0:s}", new { @class = "form-control", placeholder = "OpeningDoor", type = "datetime-local" })

Side note: type="date" and type="datetime-local" are only supported in Chrome (a normal textbox will be generated in FireFox and IE) so you should consider using a jquery plugin. Refer browser comparison.

Upvotes: 6

Related Questions