开发者

ASP.NET MVC 3 Razor jQuery Full Calendar JSON datetime format

I am implementing FullCalendar jquery with asp.net mvc 3. It's working fine but the events are not being returned to the calendar. Firebug console is telling me that they are definitely being returned, they just aren't showing up on the calendar. I think this is something to do with the date format, I've read around but can't get anything to work. Here is my controller;

public ActionResult CalendarData()
{
    DateTime myDate = DateTime.Now;

    var eventlist = from e in db.WhatsOns
                    where e.start >= myDate && e.CalenderDisplay
                    select e;

    return Json(eventlist.ToArray(), JsonRequestBehavior.AllowGet); 
}

And the javascript in my view;

<script type="text/javascript">
    $(document).ready(function() {            
       $('#calendar').fullCalendar({
    theme: true,
    header: {
        left: 'prev,next today',
       center: 'title',
        right:''

    },
    defaultView: 'month',
    editable: false,
     events: "/WhatsOn/CalendarData"
   });
}); 

</script>

The cosole output is as follo开发者_开发百科ws;

[{"ID":1,"start":"\/Date(1312844400000)\/","end":"\/Date(1313103600000)   \/","Name":"Test","Desc":"This is a test event, it will be       great!","link":"http:www.link./com","CalenderDisplay":true,"day":null,"whtscount":0,"isActive":false},{"ID":2,"start":"\/Date(1313708400000)\/","end":"\/Date(1314054000000)\/","Name":"This another test event5, it\u0027s on later than the first one.","Desc":"We aere all gpoing to get together and drink,","link":"http:www.link./com","CalenderDisplay":true,"day":null,"whtscount":0,"isActive":false}]


I had a same problem recently, not native in .net, but might be the same issue -

Full calendar requires an array whose every element has clearly defined title and start indexes, at least.

In other words, i didn't get the events shown, until i formatted the json my action was returning to something like this ( this is json from my app that works ):

events: [
    0: {sessionId:259, title:Completed regular, start:2011-08-08}
    1: {sessionId:260, title:Completed regular, start:2011-08-10}
    2: {sessionId:261, title:Not done, start:2011-08-12}
    3: {sessionId:262, title:Not done, start:2011-08-13}
    4: {sessionId:263, title:Not done, start:2011-08-15}
    5: {sessionId:264, title:Not done, start:2011-08-17}
]

Hope this helps,

Cheers


You'll find that the JSON specification does not support readily serializing datetime type objects, so given your nearest exchangeable representation, I'd suggest transmitting dates as strings (a workaround I had to use in a recent AJAX-enabled application). Using .NET's Date.Parse() method on strings generated by the JavaScript Date.toUTCString(), and calling JavaScript's Date constructor with the string generated by the native .NET Date.ToString() method, I was able to exchange datetime data easily between my .NET application and JavaScript. I would suggest this approach.

Keep in mind your application may demand UTC representations of times in your datastore if you are supporting datetime localization where your server time zone differs from application users. I'd recommend using the RFC1123 pattern specifier when calling .NET's ToString() for time localization. JavaScript's Date constructor can easily parse this format.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜