I have built a form which take checkInDate and nights as input. But in the backend when I am debugging it is not showing the date correctly. It is displaying nights correctly but checkInDate is shown as 01-01-0001.
This is index.cshtml
<div>
<form method="post"
asp-action="GetVillasByDate" >
<div class="row p-0 mx-0 py-4">
<div class="col-12 col-md-5 offset-md-1 pl-2 pr-2 pr-md-0">
<div class="form-group">
<label>Check In Date</label>
<input asp-for="CheckInDate" type="date" class="form-control" />
</div>
</div>
<div class="col-8 col-md-3 pl-2 pr-2">
<div class="form-group">
<label>No. of nights</label>
<select class="form-select" asp-for="Nights">
@for(var i = 1; i < 11; i++)
{
<option value="@i">@i</option>
}
</select>
</div>
</div>
<div class="col-4 col-md-2 pt-4 pr-2">
<div class="form-group">
<button type="button" onclick="fnLoadVillaList()" class="btn btn-success btn-block">
<i class="bi bi-search"></i> Check Availability
</button>
</div>
</div>
</div>
<partial name="_VillaList" model="Model"/>
</form>
</div>
@section scripts{
<script>
function fnLoadVillaList() {
$('.spinner').show();
// Retrieve the values using jQuery
var checkInDate = $("#CheckInDate").val();
var nights = $("#Nights").val();
console.log("CheckInDate:", checkInDate); // Debug log
console.log("Nights:", nights); // Debug log
var objData = {
checkInDate: checkInDate,
nights: $("#Nights").val()
};
$.ajax({
type: "POST",
data:objData,
url: "@Url.Action("GetVillasByDate","Home")",
success: function (data) {
$("#VillasList").empty();
$("#VillasList").html(data);
$('.spinner').hide();
},
failure: function (response) {
$('.spinner').hide();
alert(response.responseText);
},
error: function (response) {
$('.spinner').hide();
alert(response.responseText);
}
});
}
</script>
}
After I click on submit, I checked the value of checkInDate in script, it is shown as 01-01-0001 which not the input which I provided. Below is the GetVillasByDate action method
[HttpPost]
public IActionResult GetVillasByDate(int nights, DateOnly checkInDate)
{
var villaList = _unitOfWork.Villa.GetAll(IncludeProperties: "VillaAmenity").ToList();
foreach (var villa in villaList)
{
if (villa.Id % 2 == 0)
{
villa.IsAvailable = false;
}
}
HomeVM homeVM = new()
{
CheckInDate = checkInDate,
VillaList = villaList,
Nights = nights
};
return PartialView("_VillaList",homeVM);
}
And the below is the HomeVM.cs
using WhiteLagoon.Domain.Entities;
namespace WhiteLagoon.Web.ViewModels
{
public class HomeVM
{
public IEnumerable<Villa>? VillaList { get; set; }
public DateOnly CheckInDate { get; set; }
public DateOnly? CheckOutDate { get; set; }
public int Nights { get; set; }
}
}
But when I am displaying the the checkInDate in UI or logging the checkInDate it is showing correctly, but I have used the checkInDate in backend to calculate the checkOutDate , which is checkInDate added to number of nights, now the checkOutDate is not caculated correctly as checkInDate in backend is not correct.
I think there is an problem with how I am sending the date input from script, I am sending it through separate script because I wanted to update only that part of the page which is dependent on checkInDate and nights.