2012-12-05 64 views
0

我需要在我的MVC3項目中執行DHTMLX週期性事件。對於需要重複發生的事件,我需要設置我的控制器代碼來存儲和檢索額外的數據庫值,例如rec_type,event_pid。DHTMLX調度器週期性事件控制器代碼

我已經完成了簡單事件的基本編碼。但我不知道如何編寫週期性事件。演示網站本身在PHP上展示它。這是教程鏈接(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)); 
    } 

感謝。

回答

0

檢索數據應該和簡單事件一樣,除了你會渲染3個額外的字段 - event_length,event_pid,rec_type。 rec_type的默認值(這將意味着該事件不重複)是空的空字符串

至於儲蓄,勾選「控制器改變」本條第一款,它可以幫助 http://scheduler-net.com/docs/recurring_events.html#controller_changes

雖然它的目標.html的dhtmlxScheduler是獨立的組件,處理邏輯是相同的。

一般情況下,反覆出現的事件將工作方式一樣簡單的人(同樣的方法可以處理簡單的和經常性的事件),但你需要添加額外的邏輯是以下情況時的保存:

  1. 如果插入事件(rec_type ==「none」) - 響應必須具有「已刪除」狀態;
  2. 如果與事件進行了更新或刪除 - 與相關event_pid所有記錄必須刪除(string.IsNullOrEmpty(rec_type)& & rec_type = 「無」!);
  3. 如果使用非默認event_pid值的事件被刪除 - 它需要與rec_type =「無」,而不是刪除更新。

UPD,代碼示例

最後的代碼可能看起來像下面的(我沒有真正運行它,所以它可能包含一些錯誤),

所有變化,比較簡單版本,是「deleteRelated」方法(請注意,如果deleteRelated返回true,開關殼體應該省略),並在「插入」的情況下的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; 
} 
+0

在dhtmlxSched他們使用的.Net示例教程頁面內置了dll庫文件,並提供了一些提取值的函數。我無法爲燈箱設置製作自己的邏輯。你能否提供更詳細的信息?我將在這裏附上我的簡單事件代碼。 – Dheyv