ASP.NET MVC Date Input Not Binding Correctly in AJAX Post Request

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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><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>
}
</code>
<code><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> } </code>
<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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>[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);
}
</code>
<code>[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); } </code>
[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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>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; }
}
}
</code>
<code>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; } } } </code>
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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật