Best way to perform CRUD on events in the Schedule in an ASP.NET Core web app using Razor pages

I am using a Schedule to display and add/edit appointments. I have found several different ways to do this in the forums and documentation. I am using ASP.NET Core 2.1 in a web app using Razor. In one example, I found something like this:

e-data-manager url="Index/LoadData" crudUrl="Index/UpdateData" adaptor="UrlAdaptor" crossDomain="true"
This is the cshtml file for the Index page. My first question is - My LoadData and UpdateData methods are never being called. What exactly is the syntax for the 'url' and 'crudUrl' in the e-data-manager? Will this even work for a non-MVC app? The Index.cshtml is at the top level in my Pages folder in my project.
My LoadData method, in the corresponding Index.cshtml.cs file is like this:
    [HttpPost]
public List LoadData()
{
ShiftEvents = _context.Shifts.Where(s => s.StartTime > DateTime.Now.AddMonths(-1)).ToList();
return ShiftEvents;
}
It never gets called with either url="Index/LoadData" or url="LoadData" or url="/LoadData"

The documentation states:

You can map CRUD operation in grid to Server-Side Controller action using the properties insert-urlremove-urlupdate-urlcrud-url and batch-url. Does this mean that it only works in MVC? If not, how to correctly use the different properties?

1 Reply

VS Velmurugan S Syncfusion Team July 10, 2018 01:52 PM UTC

Hi Jim, 

Thanks for Contacting Syncfusion support. 

We have analyzed the reported scenario and suspect the reason for the issue might be due to the cross site request forgery attacks enabled in your application. Therefore, we request you to refer the following forum to overcome those issues. For example, adding the “IgnoreAntiforgeryToken” as shown below in your Index.cshtml.cs page will overcome the reported problem. 

  
[IgnoreAntiforgeryToken(Order = 1001)] // This is necessary to trigger the method OnPostLoadData and OnPostUpdateData 
    public class IndexModel : PageModel 
    { 
        private ScheduleDataContext _context; 
        public IndexModel(ScheduleDataContext Context) 
        { 
            _context = Context; 
        } 
} 


Also, We have prepared the sample for your requirement “Perform CRUD on events in the Schedule in an ASP.NET Core using Razor pages”, which can be downloaded from the following location. 

Please find the following code example and configuration to perform the CRUD operation properly in Schedule Control. 
Startup.cs Page: 

public void ConfigureServices(IServiceCollection services) 
        { 
            services.AddMvc().AddJsonOptions(x => { 
                x.SerializerSettings.ContractResolver = new DefaultContractResolver(); 
            }); 
            services.AddDbContext<ScheduleDataContext>(options => 
                options.UseSqlServer(Configuration.GetConnectionString("ScheduleDataConnection")));             
        } 

Index.cshtml page: 

<ejs-schedule id="schedule" height="550px"> 
    <e-schedule-eventsettings> 
        <e-data-manager url="/Index?handler=LoadData" crudUrl="/Index?handler=UpdateData" adaptor="UrlAdaptor"></e-data-manager> 
    </e-schedule-eventsettings> 
</ejs-schedule> 

Index.cshtml.cs page: 


[IgnoreAntiforgeryToken(Order = 1001)] // This is necessary to trigger the method OnPostLoadData and OnPostUpdateData 
    public class IndexModel : PageModel 
    { 
        private ScheduleDataContext _context; 
        public IndexModel(ScheduleDataContext Context) 
        { 
            _context = Context; 
        } 
 
        public void OnGet() 
        { 
 
        } 
 
        public JsonResult OnPostLoadData([FromBody]Params param) 
        { 
            var data = _context.ScheduleEvents.ToList();  
            return new JsonResult(data); 
        } 
 
        public class Params 
        { 
            public string StartDate { get; set; } 
            public string EndDate { get; set; } 
        } 
 
 
        public JsonResult OnPostUpdateData([FromBody]EditParams param) 
        { 
            if (param.action == "insert" || (param.action == "batch" && param.added.Count > 0)) // this block of code will execute while inserting the appointments 
            { 
                var value = (param.action == "insert") ? param.value : param.added[0]; 
                DateTime startTime = Convert.ToDateTime(value.StartTime); 
                DateTime endTime = Convert.ToDateTime(value.EndTime); 
                ScheduleEvent appointment = new ScheduleEvent() 
                { 
                    StartTime = startTime.ToLocalTime(), 
                    EndTime = endTime.ToLocalTime(), 
                    Subject = value.Subject, 
                    IsAllDay = value.IsAllDay, 
                    StartTimezone = value.StartTimezone, 
                    EndTimezone = value.EndTimezone, 
                    RecurrenceRule = value.RecurrenceRule, 
                    RecurrenceID = value.RecurrenceID, 
                    RecurrenceException = value.RecurrenceException, 
                    Description = value.Description, 
                    Location = value.Location 
                }; 
                _context.ScheduleEvents.Add(appointment); 
                _context.SaveChanges(); 
            } 
            if (param.action == "update" || (param.action == "batch" && param.changed.Count > 0)) // this block of code will execute while removing the appointment 
            { 
                var value = (param.action == "update") ? param.value : param.changed[0]; 
                var filterData = _context.ScheduleEvents.Where(c => c.Id == Convert.ToInt32(value.Id)); 
                if (filterData.Count() > 0) 
                { 
                    DateTime startTime = Convert.ToDateTime(value.StartTime); 
                    DateTime endTime = Convert.ToDateTime(value.EndTime); 
                    ScheduleEvent appointment = _context.ScheduleEvents.Single(A => A.Id == Convert.ToInt32(value.Id)); 
                    appointment.StartTime = startTime.ToLocalTime(); 
                    appointment.EndTime = endTime.ToLocalTime(); 
                    appointment.StartTimezone = value.StartTimezone; 
                    appointment.EndTimezone = value.EndTimezone; 
                    appointment.Subject = value.Subject; 
                    appointment.IsAllDay = value.IsAllDay; 
                    appointment.RecurrenceRule = value.RecurrenceRule; 
                    appointment.RecurrenceID = value.RecurrenceID; 
                    appointment.RecurrenceException = value.RecurrenceException; 
                    appointment.Description = value.Description; 
                    appointment.Location = value.Location; 
                } 
                _context.SaveChanges(); 
            } 
            if (param.action == "remove" || (param.action == "batch" && param.deleted.Count > 0)) // this block of code will execute while updating the appointment 
            { 
                if (param.action == "remove") 
                { 
                    int key = Convert.ToInt32(param.key); 
                    ScheduleEvent appointment = _context.ScheduleEvents.Where(c => c.Id == key).FirstOrDefault(); 
                    if (appointment != null) _context.ScheduleEvents.Remove(appointment); 
                } 
                else 
                { 
                    foreach (var apps in param.deleted) 
                    { 
                        ScheduleEvent appointment = _context.ScheduleEvents.Where(c => c.Id == apps.Id).FirstOrDefault(); 
                        if (apps != null) _context.ScheduleEvents.Remove(appointment); 
                    } 
                } 
                _context.SaveChanges(); 
            } 
            var data = _context.ScheduleEvents.ToList(); //.Where(app => (app.StartTime >= param.StartDate && app.StartTime <= param.EndDate) || (app.RecurrenceRule != null && app.RecurrenceRule != "")).ToList();  // Here filtering the events based on the start and end date value.             
            return new JsonResult(data); 
        } 
 
        public class EditParams 
        { 
            public string key { get; set; } 
            public string action { get; set; } 
            public List<ScheduleEvent> added { get; set; } 
            public List<ScheduleEvent> changed { get; set; } 
            public List<ScheduleEvent> deleted { get; set; } 
            public ScheduleEvent value { get; set; } 
        } 
    } 

Kindly try with the above sample and let us know if you need any further assistance on this. 
Regards, 
Velmurugan

Loader.
Up arrow icon