0

我已经实施了完整的日历。它确实按预期显示,但获取数据出错

.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 错误请求。知道为什么吗?

请求:

在此处输入图像描述

4

1 回答 1

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);
    }
}

结果: 在此处输入图像描述

于 2019-04-17T07:26:26.277 回答