Duplicated
Last Updated: 08 Nov 2023 15:32 by ADMIN
Nolan
Created on: 01 Nov 2023 21:42
Category: UI for Blazor
Type: Bug Report
0
Telerik.Blazor.Extensions.ObjectExtensions.Clone() Causing Issues when Initializing a Complex Model on Scheduler Edit

Sorry for the long question/request...

I'm running into an exception during a TelerikScheduler Edit event when trying to initialize the model. I believe this is occurring because the models are complex objects with another object as a property inside it, where the "submodel" does not have a parameterless constructor. I'm receiving the following exception while debugging:

I read this ticket that discusses a similar issue as well as this ticket which describes the OnModelInit event that allows you to create an instance of the model to pass to the CUD event. However, after implementing the OnModelInit handler the error is still occurring because it is trying to clone that object before passing it to the edit event and is attempting to use a constructor with no parameters which does not exist. 

To reiterate the ticket above, it would be nice if the clone process checks to see if the model implements the ICloneable interface. This will allow custom clone behavior which should resolve this issue. Or is there already a fix/workaround for this?

I added a few simplified code segments below as an example of the issue I'm having... (I'm using Blazor Server if that's relevant)

Index.razor:

@page "/"
@using TelerikBlazorTestApp.Models

<TelerikScheduler Data="@Appointments" 
                  AllowCreate="true" AllowDelete="true" AllowUpdate="true"
                  OnModelInit="@ModelInitHandler">
    <SchedulerViews>
        <SchedulerDayView />
        <SchedulerWeekView />
        <SchedulerMonthView />
    </SchedulerViews>
</TelerikScheduler>

@code {
    public List<SchedulerAppointment<TestModel>> Appointments { get; set; }

    protected override async Task OnInitializedAsync()
    {
        Appointments = new List<SchedulerAppointment<TestModel>>()
        {
             new SchedulerAppointment<TestModel> { Title = "Test 1", Start = DateTime.Now, End = DateTime.Now.AddHours(1), IsAllDay = false, Model =  new TestModel("blah") },
             new SchedulerAppointment<TestModel> { Title = "Test 2", Start = DateTime.Now.AddHours(1), End = DateTime.Now.AddHours(2), IsAllDay = false, Model =  new TestModel("blah") },
             new SchedulerAppointment<TestModel> { Title = "Test 3", Start = DateTime.Now, End = DateTime.Now, IsAllDay = true, Model =  new TestModel("blah") }
        };
    }

    public SchedulerAppointment<TestModel> ModelInitHandler()
    {
        return new SchedulerAppointment<TestModel>("blah");
    }
}

SchedulerAppointment.cs:

namespace TelerikBlazorTestApp.Models
{
    public class SchedulerAppointment<TItem>
    {
        public Guid Id { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
        public DateTime Start { get; set; }
        public DateTime End { get; set; }
        public bool IsAllDay { get; set; }
        public string RecurrenceRule { get; set; }
        public List<DateTime> RecurrenceExceptions { get; set; }
        public Guid? RecurrenceId { get; set; }

        public TItem Model { get; set; }

        public SchedulerAppointment()
        {
            Id = Guid.NewGuid();
        }

        public SchedulerAppointment(string s)
        {
            Id = Guid.NewGuid();
            Model = (TItem)Activator.CreateInstance(typeof(TItem), s);
        }
    }
}
TestModel.cs:
namespace TelerikBlazorTestApp.Models
{
    public class TestModel
    {
        public string PropA { get; set; }
        public string PropB { get; set; }

        public TestModel(string s)
        {
            // something using s here
        }
    }
}

Thanks!


Duplicated
This item is a duplicate of an already existing item. You can find the original item here:
1 comment
ADMIN
Dimo
Posted on: 08 Nov 2023 15:32

Hello Nolan,

I will relate your thread with this one about ICloneable for better visibility.

Can you describe why can't you define a parameterless constructor for TestModel? This will help us understand your scenario and needs better.

Regards,
Dimo
Progress Telerik

Stay tuned by visiting our public roadmap and feedback portal pages! Or perhaps, if you are new to our Telerik family, check out our getting started resources!