I'm a little embarrassed to ask this because, being fairly new in ASP/MVC, I started my project using the built-in scaffolded controllers/views of Visual Studio. However, I can't find anything online which has the same problem as mine so I might have missed something very basic, thanks for understanding
I haven't touch anything yet in the scaffolded Edit Action in my controller:
Controller/Edit
// GET: Tenders/Edit/5
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Tender tender = db.Tenders.Find(id);
if (tender == null)
{
return HttpNotFound();
}
ViewBag.CompendiumID = new SelectList(db.Compendia, "ID", "CTPR", tender.CompendiumID);
ViewBag.TrainerRegistrationID = new SelectList(db.TrainerRegistrations, "ID", "NTTC", tender.TrainerRegistrationID);
return View(tender);
}
Model (portion)
public int Slots { get; set; }
[DataType(DataType.Date)]
[DisplayName("Training Start")]
[DisplayFormat(DataFormatString = "{0:MMM dd, yyyy}", ApplyFormatInEditMode = true)]
public DateTime TrainingStart { get; set; }
[DataType(DataType.Date)]
[DisplayName("Training End")]
[DisplayFormat(DataFormatString = "{0:MMM dd, yyyy}", ApplyFormatInEditMode = true)]
public DateTime TrainingEnd { get; set; }
View (portion)
<div class="form-group">
@Html.LabelFor(model => model.Slots, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Slots, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Slots, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.TrainingStart, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.TrainingStart, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.TrainingStart, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.TrainingEnd, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.TrainingEnd, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.TrainingEnd, "", new { @class = "text-danger" })
</div>
</div>
Screenshot of what happens after clicking Edit
The Problem
As described above, all data can be inserted and stored in the database without encountering any problem, but when it comes to Editing, the fields containing DateTime properties are reset in the form, although cancelling the Edit won't change any previously stored data. However, it becomes troublesome when I really want to update a row because I have to re-do the dates. Is this really how the scaffolded forms work by default? or did I make a mistake somewhere? Pls help