To disable previously reserved time slots, modify your AJAX success function as follows:
success: function (result) {
//console.log(result) ;
$("#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>`);
});
// alert(result);
},
And in your backend code, you can return both available and booked slots in a single JSON object, as shown below:
public async Task<JsonResult> OnGetGetAvailableSlotsAsync(string ActivityName, string BookedDate)
{
DateTime BookingDate = Convert.ToDateTime(BookedDate);
ActivityName = ActivityName.ToString();
List<HourlyBasedView> AvailableSlots = new List<HourlyBasedView>();
List<string> BookedSlots = new List<string>();
AvailableSlots = _context.HourlyBasedView.FromSqlRaw("Select HourlyBasedTime from HalfAnHour order by id").AsNoTracking().ToList();
BookedSlots = _context.ManageBooking.Where(x => x.ActivityName == ActivityName && x.BookedDate == BookingDate).Select(x => x.PreferredTimeslot).ToList();
return new JsonResult(new { AvailableSlots, BookedSlots });
}