Home > Enterprise >  Blazor Telerik Scheduler - Unable to cast object of type 'System.String' to type 'Sys
Blazor Telerik Scheduler - Unable to cast object of type 'System.String' to type 'Sys

Time:01-06

I'm trying to populate a Telerik Blazor Scheduler UI Component with data I'm fetching from my API. The error message I'm getting is:

Error: System.AggregateException: One or more errors occurred. (Unable to cast object of type 'System.String' to type 'System.Collections.Generic.IEnumerable`1[System.DateTime]'.)

---> System.InvalidCastException: Unable to cast object of type 'System.String' to type 'System.Collections.Generic.IEnumerable`1[System.DateTime]'.

at Telerik.Blazor.Components.TelerikScheduler`1.CreateAppointment(TItem dataItem)

at Telerik.Blazor.Components.TelerikScheduler`1.ExpandAppointments()

at Telerik.Blazor.Components.TelerikScheduler`1.ProcessAppointmentsAsync()

at Telerik.Blazor.Components.TelerikScheduler`1.OnAfterRenderAsync(Boolean firstRender)

--- End of inner exception stack trace ---

Unsure about the work around for this. Below is my code:

@if (HolidayPlanners != null)
{
<pre>
 TelerikScheduler Data="@HolidayPlanners" @bind-Date="@StartDate" @bind-View="@selectedView" Height="100%" Class="Scheduler"
                  OnUpdate="@UpdateAppointment"
                  OnCreate="@AddAppointment"
                  OnDelete="@DeleteAppointment"
                  AllowCreate="true"
                  AllowDelete="true"
                  AllowUpdate="true"
                  IdField="@(nameof(UvwHolidayPlanner.Pk))"
                  StartField="@(nameof(UvwHolidayPlanner.StartDate))"
                  EndField="@(nameof(UvwHolidayPlanner.EndDate))"
                  TitleField="@(nameof(UvwHolidayPlanner.Title))"
                  DescriptionField="@(nameof(UvwHolidayPlanner.Description))"
                  IsAllDayField="@(nameof(UvwHolidayPlanner.IsAllDay))"
                  RecurrenceRuleField="@(nameof(UvwHolidayPlanner.RecurrenceRule))"
                  RecurrenceExceptionsField="@(nameof(UvwHolidayPlanner.RecurrenceExceptions))"
                  RecurrenceIdField="@(nameof(UvwHolidayPlanner.RecurrenceFk))">
    SchedulerViews
        SchedulerMonthView/SchedulerMonthView
    /SchedulerViews
/TelerikScheduler
</pre>
}

@code {


    public string _URL = String.Empty;

    IEnumerable<UvwHolidayPlanner> HolidayPlanners { get; set; }

    DateTime StartDate = DateTime.Now;

    SchedulerView selectedView { get; set; } = SchedulerView.Month;

    protected override async Task OnInitializedAsync()
    {
        _URL = settingsAccessor.AllClientSettings().BaseServiceURI;

        HolidayPlanners = (await http.CreateClient("ClientSettings").GetFromJsonAsync<List<UvwHolidayPlanner>>($"{_URL}/api/lookup/HolidayPlanner"))
        .OrderBy(t => t.Title)
        .ToList();

        StateHasChanged();

    }



    void UpdateAppointment(SchedulerUpdateEventArgs args)
    {
        //appointmentService.UpdateAppointment((AppointmentDto)args.Item);
    }

    void AddAppointment(SchedulerCreateEventArgs args)
    {
        //appointmentService.CreateAppointment((AppointmentDto)args.Item);
    }

    void DeleteAppointment(SchedulerDeleteEventArgs args)
    {
        //appointmentService.DeleteAppointment((AppointmentDto)args.Item);
    }

}

Below is UvwHoldidayPlanner class:

 public class UvwHolidayPlanner
{
    public int Pk { get; set; }
    public DateTime StartDate { get; set; }
    public DateTime EndDate { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
    public bool IsAllDay { get; set; }
    public string RecurrenceRule { get; set; }
    public int RecurrenceFk { get; set; }
    public string RecurrenceExceptions { get; set; }
    public string StartTimezone { get; set; }
    public string EndTimezone { get; set; }
}

CodePudding user response:

The property public string RecurrenceExceptions { get; set; } in your UvwHolidayPlanner class should be a List<DateTime> type and not a string.

From Telerik:

RecurrenceExceptions List A list of exceptions for a recurring appointment. It tells the Scheduler when to skip rendering a recurring appointment because its instance is explicitly changed or removed (deleted), and so it is an exception to the recurrence rule. Also see the note below.

  • Related