Waleed Naveed
Waleed Naveed

Reputation: 2381

Unable to bind datetime value with datetime control in ASP.NET MVC

I am working on an ASP.NET MVC application. My model has a DateTime field i.e.

[DisplayFormat(DataFormatString = "{0:h:mm tt}")]
[DataType(DataType.DateTime)]
public DateTime? CompletedDate { get; set; }

I have to show this in a DateTime control on front end (cshtml). My code for this in cshtml is:

@Html.TextBoxFor(m => m.model.CompletedDate, new { type = "datetime-local" })

Value of model.CompletedDate is 2020-06-15 11:00:00.000 but i don't get this value binded with my control. I get this:

enter image description here

I have to use DateTime control and can not use simple textbox. Can anyone help me. I am stuck here and didn't find anything on the google yet.

Upvotes: 0

Views: 812

Answers (1)

Mohammed Sajid
Mohammed Sajid

Reputation: 4903

The format string needs to be "{0:s}", i tried this and it's work fine for me :

@Html.TextBoxFor(m => m.model.CompletedDate,"{0:s}", new { type = "datetime-local" })

I hope you find this helpful.

Upvotes: 1

Related Questions