Add JSON as additional parameter to Update and create events in a scheduler

12 posts, 1 answers
  1. Jonas
    Jonas avatar
    39 posts
    Member since:
    Oct 2014

    Posted 15 Oct 2015 Link to this post

    Hello

    I have a scheduler with a custom edittemplate that sets the usual stuff like title, start, end and so on.
    Furthermore my template contains a javascriptfunction for adding or subtracting workers from the event and storing the result in an array called taskOperators.

    How do I pass that array to my controller action for create and update?

    .DataSource(d => d
        .Model(m =>
        {
            m.Id(f => f.TaskID);
            m.Field(f => f.Color).DefaultValue("#cc99ff");
            m.Field(f => f.Title);
            m.RecurrenceId(f => f.RecurrenceID);
        })
        .ServerOperation(true)
        .Read("Tasks_Read", "Home")
        .Create("Task_Create", "Home")
        .Destroy("Task_Delete", "Home")
        .Update("Task_Update", "Home")
    )

    I saw something about adding .Data method to the .Update and .Create but how do I bind that to my Array taskOperators?

    Best regards

    Jonas

  2. Answer
    Vladimir Iliev
    Admin
    Vladimir Iliev avatar
    2172 posts

    Posted 19 Oct 2015 Link to this post

    Hello Jonas,

    I would suggest to include the "taskOperators" as field in the ViewModel bind to the Scheduler in order to bind the values on the server side. Also you can use for example the "Save" event to save the current values to the event model - this way the changes will be sent to the server as expected. 

    Regards,
    Vladimir Iliev
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  3. UI for ASP.NET MVC is VS 2017 Ready
  4. Jonas
    Jonas avatar
    39 posts
    Member since:
    Oct 2014

    Posted 19 Oct 2015 in reply to Vladimir Iliev Link to this post

    Hello Vladimir

    Yes, after some thinking during the weekend that's the path I think I will choose

     

    /Jonas

  5. Jonas
    Jonas avatar
    39 posts
    Member since:
    Oct 2014

    Posted 19 Oct 2015 in reply to Jonas Link to this post

    Hello

    I'm having some trouble binding the taskOperators to the tasks. I have extended my TaskViewModel to look like this:

    public class TaskViewModel : ISchedulerEvent
    {
        public int TaskID { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
        public string EndTimezone { get; set; }
        public string StartTimezone { get; set; }
        public DateTime Start { get; set; }
        public DateTime End { get; set; }
        public string RecurrenceRule { get; set; }
        public int? RecurrenceID { get; set; }
        public string RecurrenceException { get; set; }
        public bool IsAllDay { get; set; }
        public int? OwnerID { get; set; }
      
        public List<BookableOperator> taskOperators { get; set; }
    }
      
    public class BookableOperator
    {
        public int TaskID { get; set; }
        public int operatorID { get; set; }
    }

    and I have altered my method for binding the data to join the tasks with the taskOperators it now looks like this:

    IQueryable<TaskViewModel> tasks = (from t in OptDB.task
                        join tom in OptDB.taskOperatorMapping on t.TaskID equals tom.TaskID
                        select t).Distinct().ToList().Select(task => new TaskViewModel()
                        {
                            TaskID = task.TaskID,
                            Start = DateTime.SpecifyKind(task.Start, DateTimeKind.Local),
                            End = DateTime.SpecifyKind(task.End, DateTimeKind.Local),
                            Description = task.Description,
                            IsAllDay = task.IsAllDay,
                            RecurrenceRule = task.RecurrenceRule,
                            RecurrenceException = task.RecurrenceException,
                            RecurrenceID = task.RecurrenceID,
                            OwnerID = task.OwnerID
                        }).AsQueryable();
     

    But I'm not sure this is the right way and how do I bind the child elements to each task?

     

    /Jonas

     

  6. Vladimir Iliev
    Admin
    Vladimir Iliev avatar
    2172 posts

    Posted 21 Oct 2015 Link to this post

    Hello Jonas,

    From the provided information it seems that what you actually need is editing of another table (navigation property) inside the edit template of the Scheduler. If this is the case than you can use the approach showed in the following demo in our documentation:

    Regards,
    Vladimir Iliev
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  7. Jonas
    Jonas avatar
    39 posts
    Member since:
    Oct 2014

    Posted 21 Oct 2015 in reply to Vladimir Iliev Link to this post

    Hello Vladimir

    Thanks, this indded looks like what i'm looking for. However I need to get more information then attendeId (operators in my project)

    I also need to get information about in which role they are booked as well as their name, so I need to extend:

    public IEnumerable<int> Attendees { get; set; }

    To be a list?

    public List<taskOperator> taskOperators { get; set; }
     
     
    public class taskOperator
        {
            public string bookedInRole { get; set; }
            public int operatorID { get; set; }
            public string operatorName { get; set; }
        }

    However I get a compilation error when I try to add taskOperators to my get method(shortened for better overview):

    IQueryable<TaskViewModel> tasks = OptDB.task.ToList().Select(task => new TaskViewModel()
                    {
                        TaskID = task.TaskID,
                        ......
                        taskOperators = new taskOperator
                        {
                            operatorID = Convert.ToInt32(task.taskOperatorMapping.Select(m => m.OperatorID)),
                            bookedInRole = task.taskOperatorMapping.Select(m => m.BookedInRole).ToString()
                        }
                    }).AsQueryable();

    Cannot implicitly convert type 'OperatorPlanningTool.Models.taskOperator' to 'System.Collections.Generic.List<OperatorPlanningTool.Models.taskOperator> 

     What am I doing wrong?

  8. Vladimir Iliev
    Admin
    Vladimir Iliev avatar
    2172 posts

    Posted 23 Oct 2015 Link to this post

    Hi Jonas,

    From the provided code it seems that you are trying to set single object to property that expects list. In current case you should either modify the taskOperator field to be of type "taskOperator" (not list) or modify the "get" method to set this field to List<taskOperator> object as follows:

    TaskID = task.TaskID,
    ......
    taskOperators = new List<taskOperator> () {
      new taskOperator
      {
        operatorID = Convert.ToInt32(task.taskOperatorMapping.Select(m => m.OperatorID)),
        bookedInRole = task.taskOperatorMapping.Select(m => m.BookedInRole).ToString()
      }
    }

    Regards,
    Vladimir Iliev
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  9. Jonas
    Jonas avatar
    39 posts
    Member since:
    Oct 2014

    Posted 26 Oct 2015 Link to this post

    Hello Vladimir

    Now at least I don't get an error but I still don't get the results i'm expecting, I have attach an image with the relevant parts of my Entity data model, a screenprint from my database showing the records I would expect to bind to my model and 2 debugging screenprints showing the binding to my viewmodel and the finished bound result. I will also post my viewmodel and the binding action once again for you to review (I have shortened both to leave out unrelevant information), I'm sorry to ask so stupid questions but i'm really stuck.

    The viewmodel:

    public class TaskViewModel : ISchedulerEvent
        {
            public int TaskID { get; set; }
     
            ....
            
            public List<taskOperator> taskOperators { get; set; }
        }
     
    public class taskOperator
        {
            public string bookedInRole { get; set; }
            public int operatorID { get; set; }
            public string operatorName { get; set; }
        }

    The action to bind values to my viewmodel:

    public JsonResult Tasks_Read([DataSourceRequest]DataSourceRequest request)
            {
                using (var OptDB = new OptDB_Entities())
                {
     
                    IQueryable<TaskViewModel> tasks = OptDB.task.ToList().Select(task => new TaskViewModel()
                    {
                        TaskID = task.TaskID,
                         
                        taskOperators = new List<taskOperator>()
                        {
                                new taskOperator{
                                
                                bookedInRole = task.taskOperatorMapping.Select(m => m.BookedInRole).ToString()
                                }
                        }
                    }).AsQueryable();
     
                    return Json(tasks.ToDataSourceResult(request));
                }
            }
     

    As you can see from the screenshots I have 3 operators linked to the taskID=3 in the taspOperatorMapping table and the LINQ statement correctly picks up all 3 of these but the bound viewmodel returns only 1 row?

    I hope this make my scenario clear to you and that you are able to guide me to why I don't get the results i'm expecting?

    Best regards

    Jonas

     

     

     

  10. Vladimir Iliev
    Admin
    Vladimir Iliev avatar
    2172 posts

    Posted 28 Oct 2015 Link to this post

    Hi Jonas,

    Please note that if you load the taskOperators from the EF context you can simply include the related entities as demonstrated in the following help article:

    Regards,
    Vladimir Iliev
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  11. Jonas
    Jonas avatar
    39 posts
    Member since:
    Oct 2014

    Posted 28 Oct 2015 in reply to Vladimir Iliev Link to this post

    Ok

     But how do I bind the takoperators for a task to the model in the Tasks_Read action?

    so far I have, my viewmodel:

    public class TaskViewModel : ISchedulerEvent
        {
            public int TaskID { get; set; }
            ...
            public virtual ICollection<taskOperator> taskOperators { get; set; }
        }
     
    public class taskOperator
        {
            public string bookedInRole { get; set; }
            public int TaskID {get; set;}
            public int operatorID { get; set; }
            public string operatorName { get; set; }
            public virtual TaskViewModel task { get; set; }
        }

    and my Tasks_Read action to populate the scheduler:

    public JsonResult Tasks_Read([DataSourceRequest]DataSourceRequest request)
            {
                using (var OptDB = new OptDB_Entities())
                {
                    IQueryable<TaskViewModel> tasks = OptDB.task.ToList().Select(task => new TaskViewModel()
                    {
                        TaskID = task.TaskID,
                        ...
                        taskOperators = ???
     
                    }).AsQueryable();
     
                    return Json(tasks.ToDataSourceResult(request));
                }
            }
     

    What do I need to write instead of taskOperators = ??? to bind the operatorID, operatorName and bookedInRole for the operators for the current task?

    there can be zero-many operators booked for each task

     

    /jonas

     

  12. Vladimir Iliev
    Admin
    Vladimir Iliev avatar
    2172 posts

    Posted 30 Oct 2015 Link to this post

    Hi,

    Please note that configuring the EntityFramework falls outside the scope of our support service as it's related to general programming knowledge. That why currently I could only suggest to start by trying the suggested changes in the related help article. 
     
    Regards,
    Vladimir Iliev
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  13. Jonas
    Jonas avatar
    39 posts
    Member since:
    Oct 2014

    Posted 04 Nov 2015 in reply to Vladimir Iliev Link to this post

    This is what I ended up with if someone else should need it.

    ViewModel:

    public class TaskViewModel : ISchedulerEvent
    {
      public int TaskID { get; set; }
      public string Title { get; set; }
      ...
      public virtual List<taskOperator> taskOperators { get; set; }
    }
    public class taskOperator
    {
      public int TaskID {get; set;}
      public int operatorID { get; set; }
      public string operatorName { get; set; }
      public string bookedInRole { get; set; }
      public TaskViewModel task { get; set; }
    }

    TasksRead Action:

    IQueryable<TaskViewModel> tasks = OptDB.task.ToList().Select(task => new TaskViewModel()
    {
        TaskID = task.TaskID,
        Title = task.productId != null ? task.product.name : task.planningGroup.name,
        taskOperators = task.taskOperatorMapping.Select(tm => new taskOperator()
        {
            bookedInRole = tm.BookedInRole,
            operatorID = tm.person.id,
            operatorName = tm.person.name
        }).ToList()
    }).AsQueryable();
     

    /Jonas

Back to Top
UI for ASP.NET MVC is VS 2017 Ready