I refer this tutorial https://github.com/bhrugen/AppointmentScheduler I did same code but I’m not able to show Get Calendar Data when I running it, It’s shows error – Unable to cast object of type ‘System.String’ to type ‘System.Boolean. My Code is :-
AppointmentApiController.cs :
JavaScript
x
31
31
1
[HttpGet]
2
[Route("GetCalendarData")]
3
public IActionResult GetCalendarData(string doctorId)
4
{
5
CommonResponse<List<AppointmentVM>> commonResponse = new CommonResponse<List<AppointmentVM>>();
6
try
7
{
8
if (role == Helper.Patient)
9
{
10
commonResponse.dataenum = _appointmentService.PatientsEventsById(loginUserId);
11
commonResponse.status = Helper.success_code;
12
}
13
else if (role == Helper.Doctor)
14
{
15
commonResponse.dataenum = _appointmentService.DoctorsEventsById(loginUserId);
16
commonResponse.status = Helper.success_code;
17
}
18
else
19
{
20
commonResponse.dataenum = _appointmentService.DoctorsEventsById(doctorId);
21
commonResponse.status = Helper.success_code;
22
}
23
}
24
catch (Exception e)
25
{
26
commonResponse.message = e.Message;
27
commonResponse.status = Helper.failure_code;
28
}
29
return Ok(commonResponse);
30
}
31
Script.js :
JavaScript
1
66
66
1
var routeURL = location.protocol + "//" + location.host;
2
$(document).ready(function () {
3
$("#appointmentDate").kendoDateTimePicker({
4
value: new Date(),
5
dateInput: false
6
});
7
8
InitializeCalendar();
9
});
10
var calendar;
11
function InitializeCalendar() {
12
try {
13
var calendarEl = document.getElementById('calendar');
14
if (calendarEl != null) {
15
calendar = new FullCalendar.Calendar(calendarEl, {
16
initialView: 'dayGridMonth',
17
headerToolbar: {
18
left: 'prev,next,today',
19
center: 'title',
20
right: 'dayGridMonth,timeGridWeek,timeGridDay'
21
},
22
selectable: true,
23
editable: false,
24
select: function (event) {
25
onShowModal(event, null);
26
},
27
eventDisplay:'block',
28
events: function (frtch, successCallback, failureCallback) {
29
$.ajax({
30
url: routeURL + '/api/Appointment/GetCalendarData?doctorId=' + $("#doctorId").val(),
31
type: 'GET',
32
dataType: 'JSON',
33
success: function (response) {
34
var events = [];
35
if (response.status === 1) {
36
$.each(response.dataenum, function (i, data) {
37
events.push({
38
title: data.title,
39
description: data.description,
40
start: data.startDate,
41
end: data.endDate,
42
backgroundColor: "#162466",
43
textColor: "white",
44
id: data.id
45
});
46
})
47
}
48
successCallback(events);
49
},
50
error: function (xhr) {
51
$.notify("Error", "error");
52
}
53
});
54
},
55
eventClick: function (info) {
56
getEventDetailsByEventId(info.event);
57
}
58
});
59
calendar.render();
60
}
61
}
62
catch (e) {
63
alert(e);
64
}
65
}
66
AppointmentService.cs :
JavaScript
1
14
14
1
public List<AppointmentVM> DoctorsEventsById(string doctorId)
2
{
3
return _db.Appointments.Where(x => x.DoctorId == doctorId).ToList().Select(c => new AppointmentVM()
4
{
5
Id = c.Id,
6
Description = c.Description,
7
StartDate = c.StartDate.ToString("yyyy-MM-dd HH:mm:ss"),
8
EndDate = c.EndDate.ToString("yyyy-MM-dd HH:mm:ss"),
9
Title = c.Title,
10
Duration = c.Duration,
11
IsDoctorApproved = c.IsDoctorApproved
12
}).ToList();
13
}
14
IAppointmentService.cs :
JavaScript
1
2
1
public List<AppointmentVM> DoctorsEventsById(string doctorId);
2
Advertisement
Answer
Unable to cast object of type ‘System.String’ to type ‘System.Boolean.
You need to make sure the type of IsDoctorApproved
in AppointmentVM
is bool:
JavaScript
1
5
1
public class AppointmentVM
2
{
3
public bool IsDoctorApproved {get;set;}
4
}
5
Also,you need to make sure the type of IsDoctorApproved
in your database is bool.