0

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

我已经完成了简单事件的基本编码。但我不知道如何为重复发生的事件写作。在演示站点本身中显示它在 PHP 上。这是教程链接(这里)。请为我提供 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));
    }

谢谢。

4

1 回答 1

0

数据的检索应该与简单事件相同,除了您将呈现 3 个额外字段 - event_length、event_pid、rec_type。rec_type 的默认值(这意味着事件不会重复发生)是 null 的空字符串

至于保存,请查看本文的“控制器更改”段落,它可能会有所帮助 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"进行更新,而不是删除。

代码示例

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

与简单版本相比,所有更改都是“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;
}
于 2012-12-06T09:19:25.293 回答