Teena
Teena

Reputation: 165

Datefield is not populating in Razor Pages

My Razor Pages edit screen is not populating datefield, all the other fields are populating correctly enter image description here

Checked database table,the format is '2020-12-29 00:00:00.000' in my model class the dateformat im passing is

        //--Start Date ---//
        [Required(ErrorMessage = "Please select Start Date")]
        [Display(Name = "Start Date*")]
        [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
        [DataType(DataType.Date)]
        public DateTime Startdate { get; set; }

Upvotes: 0

Views: 1086

Answers (2)

Amit Saini
Amit Saini

Reputation: 1

Change [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]

to

[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]

Upvotes: 0

mj1313
mj1313

Reputation: 8459

If you use asp-for tag helper for the DateTime field, the generated html element will be like this: <input type= "date" /> , by default the date format is "mm/dd/yyyy" and seems not allow other dateformat. However you could try to change it to text type.

<input asp-for="Startdate" type="text" class="datepicker" />

then use bootstrap datepicker to allow it select date.

<script src="~/lib/jquery/dist/jquery.min.js"></script>
<script src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.9.0/js/bootstrap-datepicker.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.9.0/css/bootstrap-datepicker.min.css" />

<script>
    $('.datepicker').datepicker({
        format: 'dd/mm/yyyy'
    });
</script>

Upvotes: 1

Related Questions