Similar to Datetime field in MVC Entity Framework, but not quite. Their solutions also, did not fix my issue. I have a dB with seeded data and the following,
A Model with a nullable DateTime prop:
[DataType(DataType.Time)]
[DisplayFormat(DataFormatString = "{0:hh:mm tt}", ApplyFormatInEditMode = true)]
[Display(Name = "Evening Showtime")]
public DateTime? ShowtimeEve { get; set; }
A Razor Pages View called Productions.cshtml:
@model TheatreCMS.Models.Production
@using TheatreCMS.Controllers
@{
ViewBag.Title = "Edit";
}
@Styles.Render("~/Content/Site.css")
<h2>Edit</h2>
@using (Html.BeginForm("Edit", "Productions", null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
<div class="formContainer2">
<div class="form-horizontal">
<h4>Production</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.HiddenFor(model => model.ProductionId)
<div class="form-group">
@Html.LabelFor(model => model.ShowtimeEve, htmlAttributes: new { @class = "control-label col-md-2 inputLabel" })
<div class="col-md-10 formBox">
@Html.EditorFor(model => model.ShowtimeEve, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ShowtimeEve, "", new { @class = "text-danger" })
</div>
</div>
}
And a Controller ProductionsController.cs that passes the data to the View using inheritance of DBContext (It's a code first project):
[HttpGet]
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Production production = db.Productions.Find(id);
if (production == null)
{
return HttpNotFound();
}
return View(production);
}
The form on the View will not populate the data in the input field that gets created. It's creating a blank Time picker field. I need it to be a data populated time picker field. It's an Edit View, so the goal is to have all the fields populated with the data, showing the client what they would be changing it from.
I ran the debugger, step by step, checking output values. It's passing the DateTime value to the View from the Controller, but it's getting lost somewhere after that.


Datetimeand the type that you force to specify isDatatype.timeDataType.Time, just likeDataType.Date, are inherited from the baseDataType.Datetime. At least for[DataAnnotations], that is. I know there is no "Time" data type, at least not natively, in C#. That would just be DateTime, then formatted with aDateTime.ToString("hh:mm tt").<input class="form-control text-box single-line" id="ShowtimeEve" name="ShowtimeEve" type="time" value="05:30 PM">, but after changing{0:hh:mm tt}to{0:hh:mm}in the[DataAnnotation], it sent it as a 24 hr. formatted time, which is what it needed, thus correctly rendering<input class="form-control text-box single-line" id="ShowtimeEve" name="ShowtimeEve" type="time" value="05:30">instead.