philanderson888 / c-sharp

A repository for teaching C#
MIT License
2 stars 1 forks source link

Show the date when editing an item in the FamilyDatabase application #23

Closed philanderson888 closed 4 years ago

philanderson888 commented 4 years ago

At the moment the date is blank in edit mode even though there is a valid date in the database.

Change the view logic to show the date

Should be trivial.

philanderson888 commented 4 years ago

At the moment here is the code in the model

        [Display(Name = "Date")]
        [DataType(DataType.Date)]
        [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
        public DateTime LogDate { get; set; }

I'm not sure if I have to update the model code or the view code.

Let's try updating the model code first

philanderson888 commented 4 years ago

Let's try this fix

DataFormatString = "{0:yyyy-MM-dd}"
philanderson888 commented 4 years ago

This was a solution given - does it work?

public class ObScore
{
    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
    public DateTime Date { get; set; }
}

and

<div class="form-group">
    @Html.LabelFor(model => model.Date, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">

        @* Control with date picker *@
        @Html.EditorFor(model => model.Date, new { htmlAttributes = new { @class = "form-control" } })
        @Html.ValidationMessageFor(model => model.Date, "", new { @class = "text-danger" })
    </div>
</div>
philanderson888 commented 4 years ago

And - yes - wow - just making that very small change has worked!

Problem is now fixed!!!

philanderson888 commented 4 years ago

Should update Notion now also!