locked
Date control is not showing value from model RRS feed

  • Question

  • User-615151130 posted

    I have this code

        [DataType(DataType.Date)]
            [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}",ApplyFormatInEditMode = true)]
            [Required(ErrorMessage = "Captura este campo")]
            public DateTime? BirthDate { get; set; }
     <div class="form-group">
                            <label asp-for="BirthDate" class="control-label"></label>
                            <input asp-for="BirthDate"  asp-format="{0:dd/MM/yyyy}" value="@Model.BirthDate" class="form-control" />
                            <span asp-validation-for="BirthDate" class="text-danger"></span>
                        </div>

    Im sure model is retrieving value in the right format , but is not showing in the control, so everytime i am updating the object, i must to select date again.

    Friday, June 12, 2020 3:51 AM

Answers

  • User2078676645 posted

    Hi,

    Because you set this property ApplyFormatInEditMode = true, ApplyFormatInEditMode means that its value will be cleared each time it is in the text box. So it can be set to false.

    <input asp-for="BirthDate"  asp-format="{0:dd/MM/yyyy}" value="@Model.BirthDate" class="form-control" />

    Additionally, asp-for duplicates asp-format and value. So, you can change this part of code to

     <input asp-for="@Model.BirthDate"  class="form-control" />

    Regards,

    Evern

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 12, 2020 5:31 AM

All replies

  • User2078676645 posted

    Hi,

    Because you set this property ApplyFormatInEditMode = true, ApplyFormatInEditMode means that its value will be cleared each time it is in the text box. So it can be set to false.

    <input asp-for="BirthDate"  asp-format="{0:dd/MM/yyyy}" value="@Model.BirthDate" class="form-control" />

    Additionally, asp-for duplicates asp-format and value. So, you can change this part of code to

     <input asp-for="@Model.BirthDate"  class="form-control" />

    Regards,

    Evern

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 12, 2020 5:31 AM
  • User-615151130 posted

    This solved it, thanks

    Wednesday, June 17, 2020 12:35 AM