簡體   English   中英

無法在 HTml 中獲取日期數據<input type= 'date'>從數據庫

[英]Unable to fetch date data in HTml <input type= 'date'> from database

我有一個 HTML 輸入框

<label>Holiday</label>
<input type="date"  class="form-control" asp-for="HolidayDate" >

在這個字段中,我想從我的 sqlserver 中的數據庫中獲取數據。 為此,我使用了按鈕單擊的代碼

javascript代碼

<script>`javascript`
  $(".btnGet").click(function () {
            var origin = window.location.origin;
            $.post(origin + "/HolidayMaster/Edit //my controller page
                { Id: $(this).attr("data-id") }, //id for fetching data
                function (response) {
                    $("#HolidayMasterId").val(response.holidayMasterId)`primary key`
                   $("#HolidayDate").val(response.holidayDate)
                  
                }
            );
        });

</script>

這里HolidayMaster是我的控制器

我的問題是我無法將日期帶入表單控件,但函數(響應)從數據庫中獲取數據。

.NET Datetime 與 JavaScript Date 不同,請確保轉換如下:

$(".btnGet").click(function () {
        var origin = window.location.origin;
        $.post(origin + "/HolidayMaster/Edit", 
            { Id: $(this).attr("data-id") }, //id for fetching data
            function (response) {                   
                $("#HolidayMasterId").val(response.holidayMasterId)
                $("#HolidayDate").val(new Date(Date.parse(response.holidayDate)).toDateInputValue())          
            }
        );
    });
Date.prototype.toDateInputValue = (function() {
    var local = new Date(this);
    local.setMinutes(this.getMinutes() - this.getTimezoneOffset());
    return local.toJSON().slice(0,10);
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM