我正在尝试从数据库中删除超过30天的事件。我不想使用removeEvents,因为这只会删除事件的呈现而不会从数据库中删除。或者我错了吗?
由于fullCalendar中没有deleteEvents函数,我无法像这样调用日历中的事件:示例: event.start.format()
找出活动的开始日期,以便计算是否超过30天将其删除。
我尝试制作自定义函数并将其添加到fullCalendar中,如下所示:
//-------------- DELETE EVENTS OUDER DAN 30 DAGEN--------------
deleteEvents: function(event, start, end){
var form = $('#__AjaxAntiForgeryForm');
var token = $('input[name="__RequestVerificationToken"]', form).val();
var startDate = event.start.format("YYYY-MM-DD");
var diff = newDate(Date.parse(start) - Date.parse(currentDate)); //start - current returns difference in milliseconds
var days = diff / 1000 / 60 / 60 / 24; //get days
if (days => 30) {
$.ajax({
type: "post",
url: "@Url.Action("Delete","medewerker_melding")", //send to Action, Controller
ajaxasync: true,
data: {
id: event.id, //ID of event that needs to be deleted
__RequestVerificationToken: token //ValidateAntiForgeryToken
},
success: function () {
// redirect
window.location.replace('@Url.Action("Index", "medewerker_melding")');
},
error: function (data) {
alert("Fout: verouderde events niet kunnen verwijderen.\nError: " + data);
}
});
}
}
但是fullCalendar只是忽略了它,因为它并不存在于jQuery fullCalendar中。我意识到这个剧本无论如何都不会奏效,但除此之外。
所以我希望有更多jQuery fullCalendar和jQuery经验的人知道从$(document).ready
数据库中删除超过30天的事件的方法。
谢谢。
答案 0 :(得分:1)
在与@ADyson交谈并且对我的代码大肆宣传之后,我终于解决了它
public void DeleteOldEvents()
{
temphrmEntities db = new temphrmEntities();
var datecheck = DateTime.Now.AddMonths(-1);
var q = from n in db.medewerker_melding
where n.datum_van < datecheck
group n by n.ID into g
select g.OrderByDescending(t => t.ID).FirstOrDefault();
foreach (var ev in q)
{
db.medewerker_melding.Remove(ev);
}
db.SaveChanges();
}
public ActionResult Index()
{
DeleteOldEvents();
return View();
}
谢谢@ADyson