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.