问题获取事件完整日历asp.net / c#

时间:2018-05-26 15:20:56

标签: c# asp.net json fullcalendar

首先,我会说我在这里尝试了很多样本​​,而其他人没有取得成功,所以在这里张贴建议。

我正在使用完整日历。

这是开始:

$('#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) + "'" +
             "},";

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

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

谢谢

2 个答案:

答案 0 :(得分:0)

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"
}]

答案 1 :(得分:0)

根据您更新的问题,您需要更新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) + "\"" +
             "},";