简体   繁体   中英

Datefield is not populating in Razor Pages

My Razor Pages edit screen is not populating datefield, all the other fields are populating correctly在此处输入图像描述

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; }

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM