I have a input box in HTML
<label>Holiday</label>
<input type="date" asp-for="HolidayDate" >
In this field I want data from my Database in sqlserver. For this I used the code on button click
code for 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>
Here HolidayMaster
is my controller
My question is I am unable to bring date in form control but Function(response) fetch data from Database.
CodePudding user response:
.NET Datetime is different from JavaScript Date, be sure convert like below:
$(".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);
});