DHTMLX Scheduler重复事件控制器代码

时间:2012-12-05 12:17:19

标签: asp.net-mvc-3 dhtmlx

我需要在我的MVC3项目中执行DHTMLX重复事件。对于重复发生的事件,我需要设置我的控制器代码来存储和检索额外的数据库值,如rec_type,event_pid。

我已完成简单事件的基本编码。但我不知道如何为重复发生的事件写作。在演示站点本身在PHP上显示它。这是Tutorial链接(Here)。请为我提供C#环境的逻辑。

简单事件创建/更新/删除

    public ActionResult Save(Event changedEvent, FormCollection actionValues)
    {
        var a = "Z";
        String action_type = actionValues["!nativeeditor_status"];
        Int64 source_id = Int64.Parse(actionValues["id"]);
        Int64 target_id = source_id;
        string category = actionValues["category"];
        string title = actionValues["title"];
        string description = actionValues["text"];
        if (actionValues["rec_type"] != "")
        {
            changedEvent.Rec_Type = actionValues["rec_type"];
        }
        else
            changedEvent.Rec_Type = "";
        if (actionValues["event_length"] != "")
        {
            changedEvent.Event_Length = Convert.ToInt32(actionValues["event_length"]);
        }
        else
            changedEvent.Event_Length = 0;
        if (actionValues["event_pid"] != "")
        {
            changedEvent.Event_Pid = Convert.ToInt16(actionValues["event_pid"]);
        }
        else
            changedEvent.Event_Pid = 0;
        String catg = category;
        changedEvent.UserId = 1;
        changedEvent.Category = catg;
        changedEvent.Description = description;
        changedEvent.Title = title;
        try
        {
            switch (action_type)
            {
                case "inserted":
                    changedEvent.UserId = 1;
                    changedEvent.Category = catg;
                    db.Events.AddObject(changedEvent);

                    break;
                case "deleted":
                    changedEvent = db.Events.SingleOrDefault(ev => ev.Id == source_id);
                    db.Events.DeleteObject(changedEvent);
                    break;
                default: // "updated"
                    db.Events.Attach(changedEvent);
                    db.ObjectStateManager.ChangeObjectState(changedEvent, System.Data.EntityState.Modified);
                    db.SaveChanges();
                    break;
            }
            db.SaveChanges();
            target_id = changedEvent.Id;
        }
        catch
        {
            action_type = "error";
        }

        return View(new CalendarActionResponseModel(action_type, source_id, target_id, catg));
    }

感谢。

1 个答案:

答案 0 :(得分:0)

检索数据应该与简单事件相同,除了你将渲染3个额外的字段 - event_length,event_pid,rec_type。 rec_type的默认值(这意味着该事件不再发生)是空字符串

至于保存,请查看本文的“控制器更改”段落,这可能有所帮助 http://scheduler-net.com/docs/recurring_events.html#controller_changes

虽然它是针对.Net的dhtmlxScheduler,它是独立的组件,但处理逻辑是相同的。

通常,保存重复事件的方式与简单方法相同(同一方法可以处理简单和重复事件),但是您需要为以下情况添加其他逻辑:

  1. 如果插入了带有( rec_type ==“none”)的事件,则响应必须具有“已删除”状态;
  2. 如果更新或删除了带有(!string.IsNullOrEmpty(rec_type)&& rec_type!=“none”)的事件 - 必须删除所有包含相关event_pid的记录;
  3. 如果删除了非默认 event_pid 值的事件,则需要使用 rec_type =“none”进行更新,而不是删除。
  4. UPD ,代码示例

    最终代码可能如下(我实际上没有运行它,所以它可能包含一些错误),

    与简单版本相比,所有更改都是“deleteRelated”方法(请注意,如果deleteRelated返回true,则应省略switch-case)并检查“inserted”情况下的Rec_Type

    public ActionResult Save(Event changedEvent, FormCollection actionValues){
        String action_type = actionValues["!nativeeditor_status"];
        Int64 source_id = Int64.Parse(actionValues["id"]);
        Int64 target_id = source_id;
        string category = actionValues["category"];
        string title = actionValues["title"];
        string description = actionValues["text"];
    
    
        if (!string.IsNullOrEmpty(actionValues["rec_type"]))
            changedEvent.Rec_Type = actionValues["rec_type"];
    
        if (!string.IsNullOrEmpty(actionValues["event_length"]))
            changedEvent.Event_Length = Convert.ToInt32(actionValues["event_length"]);
    
        if (!string.IsNullOrEmpty(actionValues["event_pid"]))
            changedEvent.Event_Pid = Convert.ToInt16(actionValues["event_pid"]);
    
        String catg = category;
        changedEvent.UserId = 1;
        changedEvent.Category = catg;
        changedEvent.Description = description;
        changedEvent.Title = title;
        try
        {
            if (!deleteRelated(action_type, changedEvent, db))//some logic specific for recurring events
            {
                switch (action_type)
                {
                    case "inserted":
                        changedEvent.UserId = 1;
                        changedEvent.Category = catg;
                        db.Events.AddObject(changedEvent);
    
                        if (changedEvent.Rec_Type == "none")
                            action_type = "deleted";//if an event with (rec_type == "none") was inserted - the response must have "deleted" status
    
                        break;
                    case "deleted":
                        changedEvent = db.Events.SingleOrDefault(ev => ev.Id == source_id);
                        db.Events.DeleteObject(changedEvent);
                        break;
                    default: // "updated"
                        db.Events.Attach(changedEvent);
                        db.ObjectStateManager.ChangeObjectState(changedEvent, System.Data.EntityState.Modified);
                        db.SaveChanges();
                        break;
                }
            }
            db.SaveChanges();
            target_id = changedEvent.Id;
        }
        catch
        {
            action_type = "error";
        }
    
        return View(new CalendarActionResponseModel(action_type, source_id, target_id, catg));
    }
    protected bool deleteRelated(string action_type, Event changedEvent, RecurringEntities db)//
    {
        bool finished = false;
        if ((action_type == "deleted" || action_type == "updated")
            && !string.IsNullOrEmpty(changedEvent.Rec_Type))
        {
            //if an event with (!string.IsNullOrEmpty(rec_type) && rec_type != "none") was updated or deleted - all records with the related event_pid must be deleted;
            db.ExecuteStoreCommand("DELETE FROM Films WHERE Event_Pid = {0}", changedEvent.Id);
        }
        if (action_type == "deleted"
                && (changedEvent.Event_Pid != 0 && changedEvent.Event_Pid != default<Int16>))
        {
            //if an event with non-default event_pid value was deleted - it need to be updated with rec_type = "none" instead of deleting.
            var item = db.Events.SingleOrDefault(ev => ev.Id == changedEvent.Id);
            item.Rec_Type = "none";
            db.SaveChanges();
            finished = true; //in this case no more processing is needed
        }
        return finished;
    }