首页 > 解决方案 > 问题获取事件完整日历 asp.net / c#

问题描述

首先,我会说我在这里尝试了很多样本​​,但其他样本没有成功,所以在这里发布建议。

我正在使用完整日历。

这是开始:

$('#calendar')
     .fullCalendar({
         allDaySlot: false,
         customButtons: {
             reload: {
                 text: '+1 Year',
                 click: function() {
                     $('#calendar').fullCalendar('gotoDate', new Date(new Date().setFullYear(new Date().getFullYear() + 1)));
                 }
             }                           
         },     
         themeSystem: 'bootstrap4',
         defaultView: 'agendaWeek',
          eventClick: updateEvent,
          selectable: true,
          selectHelper: true,
         events: "JsonResponse.ashx", 
         and other attributes.....

问题是日历没有显示事件。

JsonResonse.ashx 返回如下字符串:

[{id: '1029',title: 'mr 1',clientphone: '1234556654',clientemail: 'mr1@gmail.com',start:  '2018-05-21 10:00',end: '2018-05-21 11:45',allDay:false,description: 'New test on a new calender'},{id: '1030',title: 'mr 2',clientphone: '123456',clientemail: 'mr2@gmail.com',start:  '2018-05-25 09:00',end: '2018-05-25 11:45',allDay:false,description: 'i like pringles'}]

但是在 Firefox 上,我看到它不会使事件出现一些错误:

SyntaxError: JSON.parse: expected property name or '}' at line 1 column 3 of the JSON data

所以我使用和替换事件:“JsonResponse.ashx”,返回字符串

 [{id: '1029',title: 'mr 1',clientphone: '1234556654',clientemail: 'mr1@gmail.com',start:  '2018-05-21 10:00',end: '2018-05-21 11:45',allDay:false,description: 'New test on a new calender'},{id: '1030',title: 'mr 2',clientphone: '123456',clientemail: 'mr2@gmail.com',start:  '2018-05-25 09:00',end: '2018-05-25 11:45',allDay:false,description: 'i like pringles'}]

使 JSON 返回的代码:

 return "{" +
             "id: '" + cevent.id + "'," +
             "title: '" + HttpContext.Current.Server.HtmlEncode(cevent.title) + "'," +
             "clientphone: '" + HttpContext.Current.Server.HtmlEncode(cevent.clientphone) + "'," +
             "clientemail: '" + HttpContext.Current.Server.HtmlEncode(cevent.clientemail) + "'," +
             "start:  '" + (cevent.start).ToString("yyyy-MM-dd HH:mm") + "'," +
             "end: '" + (cevent.end).ToString("yyyy-MM-dd HH:mm") + "'," +
             "allDay:" + allDay + "," +
             "description: '" + HttpContext.Current.Server.HtmlEncode(cevent.description) + "'" +
             "},";

基本上我结合所有事件然后返回。

就像那样,它工作正常并将事件放在日历上。我在这里做错了什么?

谢谢

标签: c#asp.netjsonfullcalendar

解决方案


JsonResponse.ashx 处理程序返回的 json 无效。你是如何生成json的?我建议使用NewtonSoft使用JsonConvert.SerializeObject方法将您的事件列表序列化为 json

这是您的 ashx 处理程序应返回的有效 json。请注意属性名称和值周围的双引号。

[{
    "id": "1029",
    "title": "mr1",
    "clientphone": "1234556654",
    "clientemail": "mr1@gmail.com",
    "start": "2018-05-2110: 00",
    "end": "2018-05-2111: 45",
    "allDay": false,
    "description": "Newtestonanewcalender"
},
{
    "id": "1030",
    "title": "mr2",
    "clientphone": "123456",
    "clientemail": "mr2@gmail.com",
    "start": "2018-05-2509: 00",
    "end": "2018-05-2511: 45",
    "allDay": false,
    "description": "ilikepringles"
}]

推荐阅读