0

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

View/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

imsome1
  • 1,182
  • 4
  • 22
  • 37
Lwrnc Crz
  • 43
  • 1
  • 10
  • It need to be `DataFormatString = "{0:yyyy-MM-dd}"` (ISO format) –  Oct 04 '17 at 11:10
  • Since the HTML-5 datepicker is only supported in Chrome and Edge, I suggest you use a jQuery datepicker to give consistent UI –  Oct 04 '17 at 11:13
  • It resolves the Edit issue, thank you. Is there another way to display my dates in the format "{0:MMM dd, yyyy}" other than using DataFormatString in the Model? I'm trying to change the display format in the view, but could get this to work: @Html.DisplayFor(modelItem => item.TrainingStart, "{ 0:MMM dd, yyyy}") – Lwrnc Crz Oct 04 '17 at 12:43
  • ok I got it with @item.TrainingStart.ToString("MMM dd, yyyy"), thanks again – Lwrnc Crz Oct 04 '17 at 12:59

0 Answers0