We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Creating multiple resources from the Controller and binding to the schedule control.

Hi,

I am trying to create a list of resources dynamically from in the controller, but unable to get the schedule control to bind correctly. It just shows an empty form with now schedule control rendered.

I have created a simple MVC5 project with the code shown below. Any help getting this working would be greatly appreciated. 

cshtml File contents

@(Html.EJ().Schedule("Schedule1")
        .Width("100%")
        .Height("525px")
        .Orientation(Orientation.Horizontal)  
        .CurrentDate(new DateTime(2015, 11, 5))
        .Group(gr => { gr.Resources(ViewBag.Group); })
        .Resources(res =>
         {
             res.Field("ProductCode").Title("Owner").Name("Owners").AllowMultiple(true).ResourceSettings(flds => flds.Datasource((IEnumerable)ViewBag.Owner).Text("Text").Id("Id")).Add();
         })               
        .AppointmentSettings(fields => fields.Datasource(ds => ds.URL("TestSchedular/GetData").Adaptor(AdaptorType.UrlAdaptor))                                       
        .Id("AllocationID")
        .Subject("ProductCode")
        .StartTime("AllocationStartDttm")
        .EndTime("AllocationFinishDttm")
        .Description("Comment")
        .Recurrence("Recurrence")
        .RecurrenceRule("RecurrenceRule")
        .ResourceFields("ProductCode"))            
)     

@Html.EJ().ScriptManager()


Controller cs file contents

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Web..Controllers
{
    public class TestSchedularController : Controller
    {
        List<ResourceFields> Owner = new List<ResourceFields>();
        List<string> _grpItems = new List<string>();
        
        // To initially bind the appointments with Scheduler
        public JsonResult GetData()
        {
            IEnumerable<Appointment> data = GetList();
            _grpItems = data.Select(i => i.ProductCode).Distinct().ToList();
            foreach (string item in _grpItems)
            {
                Owner.Add(new ResourceFields { Text = item, Id = item, Color = "#f8a398", GroupId = "1" });
            }
            ViewBag.Owner = Owner;

            List<String> Group = new List<String>();
            Group.Add("Owners");
            ViewBag.Group = Group;
        
            return Json(data, JsonRequestBehavior.AllowGet);
        }


        private List<Appointment> GetList()
        {
            List<Appointment> appoint = new List<Appointment>();
            appoint.Add(
                new Appointment
                    {
                        AllocationID = 1,
                        Status = "Allocated",
                        AllocationStartDttm = new DateTime(2015, 11, 05, 10, 00, 00),
                        AllocationFinishDttm = new DateTime(2015, 11, 05, 11, 00, 00),
                        Comment = "Some comments 1",
                        PickupLocationCode = "WLG",
                        DropoffLocationCode = "WLG",
                        ProductCode = "Prod 1"
                    });
            appoint.Add(
                    new Appointment
                    {
                        AllocationID = 2,
                        Status = "Transfer",
                        AllocationStartDttm = new DateTime(2015, 11, 05, 11, 00, 00),
                        AllocationFinishDttm = new DateTime(2015, 11, 05, 11, 25, 00),
                        Comment = "Some cooments 2",
                        PickupLocationCode = "WLG",
                        DropoffLocationCode = "CHC",
                        ProductCode = "Prod 1"

                    });
            appoint.Add(
              new Appointment
              {
                  AllocationID = 3,
                  Status = "Allocated",
                  AllocationStartDttm = new DateTime(2015, 11, 05, 10, 00, 00),
                  AllocationFinishDttm = new DateTime(2015, 11, 05, 11, 00, 00),
                  Comment = "Some comments 1",
                  PickupLocationCode = "WLG",
                  DropoffLocationCode = "PML",
                  ProductCode = "Prod 3"
              });
            appoint.Add(
           new Appointment
           {
               AllocationID = 6,
               Status = "Allocated",
               AllocationStartDttm = new DateTime(2015, 11, 03, 8, 00, 00),
               AllocationFinishDttm = new DateTime(2015, 11, 05, 10, 00, 00),
               Comment = "Some comments 1",
               PickupLocationCode = "WLG",
               DropoffLocationCode = "PML",
               ProductCode = "Prod 3"
           });
            appoint.Add(
              new Appointment
              {
                  AllocationID = 4,
                  Status = "Available",
                  AllocationStartDttm = new DateTime(2015, 10,29, 10, 00, 00),
                  AllocationFinishDttm = new DateTime(2015, 11, 7, 11, 00, 00),
                  Comment = "Some comments 1",
                  PickupLocationCode = "WLG",
                  DropoffLocationCode = "PML",
                  ProductCode = "Prod 5"
              });

            appoint.Add(
            new Appointment
            {
                AllocationID = 5,
                Status = "Available",
                AllocationStartDttm = new DateTime(2015, 11, 05, 11, 25, 00),
                AllocationFinishDttm = new DateTime(2015, 11, 07, 15, 35, 00),
                Comment = "Some comments 1",
                PickupLocationCode = "WLG",
                DropoffLocationCode = "PML",
                ProductCode = "Prod 1"
            });

            return appoint;
        }       
    }


    public class Appointment
    {
        public int AllocationID { get; set; }
        public string Subject { get; set; }
        public string Status { get; set; }
        public string ProductCode { get; set; }
        public DateTime AllocationStartDttm { get; set; }
        public DateTime AllocationFinishDttm { get; set; }     
        public string Comment { get; set; }
        public string PickupLocationCode { get; set; }
        public string DropoffLocationCode { get; set; }
    }

   public class ResourceFields
   {
       public string Id { get; set; }
       public string Text { get; set; }
       public string Color { get; set; }
       public string GroupId { get; set; }                         
   }

}

1 Reply

KK Karthigeyan Krishnamurthi Syncfusion Team May 9, 2016 09:31 AM UTC

Hi Mahindra,   
   
Thank you for contacting Syncfusion support.   
   
We have prepared the sample for your requirement “Need to bind the resource from controller” which can be download from the following location:   
   
In the above sample, initially we have rendered the resource from the controller side into Scheduler. Kindly refer to the following code example used in the above sample.   
  
<Code>   
HomeController.cs 
public ActionResult Index() 
        { 
            owner.Add(new Rooms { text = "Nancy", id = 1, groupId = 1, color = "#ffaa00" }); 
            owner.Add(new Rooms { text = "Stephen", id = 2, groupId = 1, color = "#f8a398" }); 
            ViewBag.Owners = owner; 
            List<String> resources = new List<String>(); 
             resources.Add("Owners"); 
            ViewBag.Resources = resources; 
            return View(); 
        } 
Index.cshtml 
.Resources(res => 
        { 
            res.Field("OwnerId").Title("Owner").Name("Owners").AllowMultiple(true) 
            .ResourceSettings(flds => flds.Datasource((System.Collections.IEnumerable)ViewBag.Owners).Text("text").Id("id").GroupId("groupId").Color("color")).Add(); 
        }) 
        .Group(gr => 
        { 
            gr.Resources(ViewBag.Resources); 
        }) 
</Code> 
 
Please refer the below link to know more about the resource data binding.   
   
Regards,   
Karthigeyan   


Loader.
Up arrow icon