从db为格式化FullCalendar事件的JSON字符串的正确方法是什么

时间:2012-07-26 18:51:00

标签: asp.net-mvc fullcalendar

我试图在我的MVC应用程序中呈现FullCalendar(1.5.3),如下所示:

$('#calendar').fullCalendar({
    header: {
        left: 'prev,next today',
        center: 'title',
        right: 'month,basicWeek,basicDay'
    },
    titleFormat: { day: '' },
    defaultView: 'month',
    events: "/Schedule/GetCal"
});

JSON生成的/Schedule/GetCal字符串如下所示:

{id:"5BFAA9C3-9437-49B0-A657-5DA47CDEA409",projectId:"5BFAA9C3-9437-49B0-A657-5DA47CDEA409",title:"Orem City Parking Lot",start:"2012-07-11",end:"",allDay:"true",type:"goal",textColor:"white",backgroundColor:"green",borderColor:"black",crew:""},
{id:"33910A42-C5F0-42FA-AB36-C315BDDAF964",projectId:"33910A42-C5F0-42FA-AB36-C315BDDAF964",title:"Thanksgiving Point - Buster",start:"2012-07-28",end:"",allDay:"true",type:"goal",textColor:"white",backgroundColor:"green",borderColor:"black",crew:""}

它不会在页面上呈现任何事件。但是,当我这样称呼时:

$('#calendar').fullCalendar({
    header: {
        left: 'prev,next today',
        center: 'title',
        right: 'month,basicWeek,basicDay'
    },
    titleFormat: { day: '' },
    defaultView: 'month',
    events: [{id:"5BFAA9C3-9437-49B0-A657-5DA47CDEA409",projectId:"5BFAA9C3-9437-49B0-A657-5DA47CDEA409",title:"Orem City Parking Lot",start:"2012-07-11",end:"",allDay:"true",type:"goal",textColor:"white",backgroundColor:"green",borderColor:"black",crew:""},
            {id:"33910A42-C5F0-42FA-AB36-C315BDDAF964",projectId:"33910A42-C5F0-42FA-AB36-C315BDDAF964",title:"Thanksgiving Point - Buster",start:"2012-07-28",end:"",allDay:"true",type:"goal",textColor:"white",backgroundColor:"green",borderColor:"black",crew:""}
   ]
});

它按预期工作。我需要更改JSON字符串的格式?或者,我怎么称呼它?

2 个答案:

答案 0 :(得分:0)

您的/Schedule/GetCal脚本需要返回一系列事件 - 用JSON字符串中事件集周围的方括号表示。

目前,您似乎只是返回一个逗号分隔的事件列表,这些事件未被解释为您的预期。

您的输出需要如下所示:

  

[“0”,{“allDay”:“”,“title”:“Go To Work”,“id”:“821”,“end”:“2011-06-06 14:00:00” ,“start”:“2011-06-06 06:00:00”},“1”,{“allDay”:“”,“title”:“Toms的派对”,“id”:“822”,“结束“:”2011-06-10 21:00:00“,”开始“:”2011-06-10 16:00:00“}]

答案 1 :(得分:0)

本文帮助我弄清楚:ASP.Net MVC, jQuery and FullCalendar

显然,对于FullCalendar 1.5.3和asp.net mvc,您需要返回已转换为List<>的{​​{1}},然后在.ToArray()中序列化。一旦我这样做,结果就像我预期的那样出现在日历中。

我使用JSON在浏览器中查看结果,而不是我在上面发布的字符串

alert(data)