You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Blazor Telerik Scheduler类型转换错误:无法将String转为IEnumerable<DateTime>

解决Telerik Blazor Scheduler字符串转IEnumerable类型转换错误

错误根源

错误提示明确指出无法将System.String转换为System.Collections.Generic.IEnumerable<System.DateTime>,问题出在重复预约例外日期的字段绑定上:Telerik Blazor Scheduler要求RecurrenceExceptionsField绑定的属性类型必须是可枚举的日期集合(IEnumerable<DateTime>),但你的UvwHolidayPlanner类中RecurrenceExceptions字段是string类型,导致组件处理数据时类型不匹配。


解决方案

方案1:直接修改实体类字段类型(推荐,若API/数据库允许)

将UvwHolidayPlanner中的RecurrenceExceptions字段类型改为IEnumerable<DateTime>,同时确保API返回的对应数据是日期数组格式(如JSON中的["2024-05-15", "2024-05-22"]),这样GetFromJsonAsync会自动解析为日期集合。

修改后的实体类代码:

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; }
    // 改为IEnumerable<DateTime>类型匹配Scheduler要求
    public IEnumerable<DateTime> RecurrenceExceptions { get; set; }
    public string StartTimezone { get; set; }
    public string EndTimezone { get; set; }
}

方案2:保留字符串类型,通过计算属性转换(无法修改API/数据库时使用)

如果不能修改RecurrenceExceptions的字符串类型,可在实体类中添加计算属性,将字符串解析为IEnumerable<DateTime>,再绑定该属性到Scheduler的RecurrenceExceptionsField。

步骤1:修改实体类添加计算属性

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; }

    // 计算属性:将逗号分隔的日期字符串解析为DateTime集合
    public IEnumerable<DateTime> RecurrenceExceptionsParsed
    {
        get
        {
            if (string.IsNullOrWhiteSpace(RecurrenceExceptions))
                return Enumerable.Empty<DateTime>();
            
            // 假设字符串格式为"2024-05-15,2024-05-22",可根据实际格式调整解析逻辑
            return RecurrenceExceptions.Split(',')
                .Where(s => !string.IsNullOrWhiteSpace(s))
                .Select(s => DateTime.Parse(s))
                .ToList();
        }
    }
}

步骤2:修改Scheduler的字段绑定

<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.RecurrenceExceptionsParsed))"
                  RecurrenceIdField="@(nameof(UvwHolidayPlanner.RecurrenceFk))">
    <SchedulerViews>
        <SchedulerMonthView />
    </SchedulerViews>
</TelerikScheduler>

额外检查

确认RecurrenceIdField绑定的RecurrenceFk类型与IdField的Pk类型一致(均为int),当前代码中该配置是正确的,无需调整。

内容的提问来源于stack exchange,提问作者bgcode

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 22:55:39