通过 json 和 ajax 从控制器获取事件到完整日历
Get events from Controller to Full calendar via json and ajax
我正在处理日历模块,但无法从控制器中将事件加载到完整日历。这是我的代码。
这是控制器class:
public JsonResult PublicHolidayList()
{
List<Event> events = new List<Event>();
events.Add(new Event()
{
EventName = "EventName 1",
StartDate = DateTime.Now.ToString("MM-dd-yyyy"),
EndDate = DateTime.Now.AddDays(2).ToString("MM-dd-yyyy")
});
events.Add(new Event()
{
EventName = "EventName 2",
StartDate = DateTime.Now.AddDays(4).ToString("MM-dd-yyyy"),
EndDate = DateTime.Now.AddDays(5).ToString("MM-dd-yyyy")
});
events.Add(new Event()
{
EventName = "EventName 3",
StartDate = DateTime.Now.AddDays(10).ToString("MM-dd-yyyy"),
EndDate = DateTime.Now.AddDays(11).ToString("MM-dd-yyyy")
});
events.Add(new Event()
{
EventName = "EventName 4",
StartDate = DateTime.Now.AddDays(22).ToString("MM-dd-yyyy"),
EndDate = DateTime.Now.AddDays(25).ToString("MM-dd-yyyy")
});
return Json(events, JsonRequestBehavior.AllowGet);
}
这是脚本:
$(function () {
$.ajax({
type: "POST",
contentType: "application/json",
data: "{}",
url: "/Leave/PublicHolidayList",
dataType: "json",
success: function(data) {
alert(data);
$('#calendar_ph').fullCalendar({
header: {
left: 'prev,next today',
center: 'title',
right: 'month,agendaWeek,agendaDay'
},
defaultView: 'month',
editable: true,
events: data,
height: 650
});
},
error: function() {
}
});
})
.done(function (data) {
});
谁能解释一下为什么我的日历中没有显示活动?
好吧,我以前遇到过这个问题,并用下面的技术解决了。所以试试这个
您需要添加 moment.js,此处可能有必要
events: $.map(data, function (item, i) {
var event = new Object();
event.title = item.EventName;
event.start = moment(item.StartDate).utc();
event.end = moment(item.EndDate).utc();
return event;
})
有关更详细的说明,请访问
我正在处理日历模块,但无法从控制器中将事件加载到完整日历。这是我的代码。
这是控制器class:
public JsonResult PublicHolidayList()
{
List<Event> events = new List<Event>();
events.Add(new Event()
{
EventName = "EventName 1",
StartDate = DateTime.Now.ToString("MM-dd-yyyy"),
EndDate = DateTime.Now.AddDays(2).ToString("MM-dd-yyyy")
});
events.Add(new Event()
{
EventName = "EventName 2",
StartDate = DateTime.Now.AddDays(4).ToString("MM-dd-yyyy"),
EndDate = DateTime.Now.AddDays(5).ToString("MM-dd-yyyy")
});
events.Add(new Event()
{
EventName = "EventName 3",
StartDate = DateTime.Now.AddDays(10).ToString("MM-dd-yyyy"),
EndDate = DateTime.Now.AddDays(11).ToString("MM-dd-yyyy")
});
events.Add(new Event()
{
EventName = "EventName 4",
StartDate = DateTime.Now.AddDays(22).ToString("MM-dd-yyyy"),
EndDate = DateTime.Now.AddDays(25).ToString("MM-dd-yyyy")
});
return Json(events, JsonRequestBehavior.AllowGet);
}
这是脚本:
$(function () {
$.ajax({
type: "POST",
contentType: "application/json",
data: "{}",
url: "/Leave/PublicHolidayList",
dataType: "json",
success: function(data) {
alert(data);
$('#calendar_ph').fullCalendar({
header: {
left: 'prev,next today',
center: 'title',
right: 'month,agendaWeek,agendaDay'
},
defaultView: 'month',
editable: true,
events: data,
height: 650
});
},
error: function() {
}
});
})
.done(function (data) {
});
谁能解释一下为什么我的日历中没有显示活动?
好吧,我以前遇到过这个问题,并用下面的技术解决了。所以试试这个
您需要添加 moment.js,此处可能有必要
events: $.map(data, function (item, i) {
var event = new Object();
event.title = item.EventName;
event.start = moment(item.StartDate).utc();
event.end = moment(item.EndDate).utc();
return event;
})
有关更详细的说明,请访问