According to your provider code and data, I create a sample using the following code, it seems that your code works well.
booking.cshtml.cs
public class BookingModel : PageModel
{
public void OnGet()
{
}
public async Task<JsonResult> OnGetGetAvailableSlotsAsync(string ActivityName, string BookedStartDate, string BookedEndDate)
{
//DateTime BookingDate = Convert.ToDateTime(BookedDate);
//ActivityName = ActivityName.ToString();
List<string> BookedSlots = new List<string>();
List<HourlyBasedView> AvailableSlots = new List<HourlyBasedView>()
{
#region
new HourlyBasedView() { hourlyBasedTime ="00.00 - 00.30" },
...
new HourlyBasedView() { hourlyBasedTime ="23.30 - 23.59" },
#endregion
};
//based on the entered date to filter the BookedSlots.
var filterdate = AvailableSlots.Where(c => (Convert.ToDouble(c.hourlyBasedTime.Split(" - ")[0]) >= Convert.ToDouble(BookedStartDate))
&& (Convert.ToDouble(c.hourlyBasedTime.Split(" - ")[1]) <= Convert.ToDouble(BookedEndDate))).Select(c=>c.hourlyBasedTime).ToList();
BookedSlots.AddRange(filterdate);
return new JsonResult(new { AvailableSlots, BookedSlots});
}
}
Booking.cshtml:
@page
@model Core7RazorSample.Pages.BookingModel
<div class="row">
<div class="col-md-4">
<form method="post">
<div class="form-group">
<label for="BookedStartTime" class="control-label">Blocked Start Time</label>
<input id="BookedStartTime" name="BookedStartTime" class="form-control" value="" />
</div>
<div class="form-group">
<label for="BookedEndTime" class="control-label">Blocked End Time</label>
<input id="BookedEndTime" name="BookedEndTime" class="form-control" value="" />
</div>
<div class="form-group">
<input type="button" id="btn" value="Show Prefered Time Slot" />
</div>
<div class="form-group">
<div id="PreferredTimeSlot">
</div>
</div>
</form>
</div>
</div>
@section Scripts{
<script>
$(function(){
$("#btn").click(function(){
var bookstartdate = $("#BookedStartTime").val();
var bookenddate = $("#BookedEndTime").val();
$.ajax({
type: "Get",
url: "/booking?handler=GetAvailableSlots",
headers: { "RequestVerificationToken": $('input[name="__RequestVerificationToken"]').val() },
data: { ActivityName: "aa", BookedStartDate: bookstartdate, BookedEndDate : bookenddate },
dataType: "json",
success: function (result) {
console.log(result);
console.log(result.AvailableSlots);
console.log(result.BookedSlots);
$("#PreferredTimeSlot").html("");
$("#PreferredTimeSlot").append('<label class="control-label">Preferred Timeslot</label><br/>');
$("#PreferredTimeSlot").css("display", "block"); //clear the container.
$.each(result.availableSlots, function (key, value) {
let disabled = result.bookedSlots.includes(value.hourlyBasedTime) ? "disabled" : "";
$("#PreferredTimeSlot").append(`<label style="white-space:pre"><input type="checkbox" class="form-check-inline mr-2" value="${value.hourlyBasedTime}" name="ManageBooking.PreferredTimeslot" ${disabled} />${value.hourlyBasedTime} <span></span></label>`);
});
}
});
});
})
</script>
}
Then the result as below, as we can see that relate checkboxes are disabled.
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best regards,
Dillion