我需要在我的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));
}
感谢。
答案 0 :(得分:0)
检索数据应该与简单事件相同,除了你将渲染3个额外的字段 - event_length,event_pid,rec_type。 rec_type的默认值(这意味着该事件不再发生)是空字符串
至于保存,请查看本文的“控制器更改”段落,这可能有所帮助 http://scheduler-net.com/docs/recurring_events.html#controller_changes
虽然它是针对.Net的dhtmlxScheduler,它是独立的组件,但处理逻辑是相同的。
通常,保存重复事件的方式与简单方法相同(同一方法可以处理简单和重复事件),但是您需要为以下情况添加其他逻辑:
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;
}