我已经实施了完整的日历。它确实按预期显示,但是获取数据出错
.cshtml
<div id='calendar'></div>
.cshtml.cs
public class IndexModel : PageModel
{
public JsonResult OnPost(DateTime start, DateTime end)
{
return new JsonResult(new
{
url = "something",
title = "something else",
start = ConvertToUnixTimestamp(DateTime.Now).ToString(),
end = ConvertToUnixTimestamp(DateTime.Now.AddDays(2)).ToString(),
allDay = false,
backgroundColor = "red",
textColor = "green"
});
}
}
.js
document.addEventListener('DOMContentLoaded', function () {
var calendarEl = document.getElementById('calendar');
var calendar = new FullCalendar.Calendar(calendarEl, {
events: {
url: '/Overview/Employee/Index',
beforeSend: function (xhr) {
xhr.setRequestHeader("XSRF-TOKEN",
$('input:hidden[name="__RequestVerificationToken"]').val());
},
method: 'POST'
},
plugins: ['dayGrid']
});
calendar.render();
});
当我加载页面时,我看到一个请求正在发生,但是它返回了400个错误的请求。知道为什么吗?
请求:
答案 0 :(得分:0)
我尝试了没有400错误的代码(在表单中使用@Html.AntiForgeryToken()
),但是该事件未成功添加到日历中。当我返回列表模型时,它可以工作,请尝试使用以下代码添加事件:
public class EventModel
{
public int id { get; set; }
public string start { get; set; }
public string end { get; set; }
public string title { get; set; }
public bool allDay { get; set; }
public string url { get; set; }
public string color { get; set; }
public string textColor { get; set; }
}
public class IndexModel : PageModel
{
public JsonResult OnPost(DateTime start, DateTime end)
{
IEnumerable<EventModel> events = new List<EventModel>()
{
new EventModel()
{
url = "something",
title = "something else",
start = (DateTime.Now).ToString(),
end = (DateTime.Now.AddDays(2)).ToString(),
allDay = false,
color = "red",
textColor = "green"
}
};
return new JsonResult(events);
}
}