2015-06-12 59 views
0

我正在處理日曆模塊,並且無法將事件從控制器加載到完整日曆。這是我的代碼。通過json和ajax從控制器獲取事件到完整日曆

這裏的控制器類:

public JsonResult PublicHolidayList() 
     { 
      List<Event> events = new List<Event>(); 
      events.Add(new Event() 
      { 

       EventName = "EventName 1", 
       StartDate = DateTime.Now.ToString("MM-dd-yyyy"), 
       EndDate = DateTime.Now.AddDays(2).ToString("MM-dd-yyyy") 
      }); 
      events.Add(new Event() 
      { 

       EventName = "EventName 2", 
       StartDate = DateTime.Now.AddDays(4).ToString("MM-dd-yyyy"), 
       EndDate = DateTime.Now.AddDays(5).ToString("MM-dd-yyyy") 
      }); 
      events.Add(new Event() 
      { 

       EventName = "EventName 3", 
       StartDate = DateTime.Now.AddDays(10).ToString("MM-dd-yyyy"), 
       EndDate = DateTime.Now.AddDays(11).ToString("MM-dd-yyyy") 
      }); 
      events.Add(new Event() 
      { 

       EventName = "EventName 4", 
       StartDate = DateTime.Now.AddDays(22).ToString("MM-dd-yyyy"), 
       EndDate = DateTime.Now.AddDays(25).ToString("MM-dd-yyyy") 
      }); 
      return Json(events, JsonRequestBehavior.AllowGet); 
     } 

這是腳本:

$(function() { 
    $.ajax({ 
     type: "POST", 
     contentType: "application/json", 
     data: "{}", 
     url: "/Leave/PublicHolidayList", 
     dataType: "json", 
     success: function(data) { 
      alert(data); 
      $('#calendar_ph').fullCalendar({ 
       header: { 
        left: 'prev,next today', 
        center: 'title', 
        right: 'month,agendaWeek,agendaDay' 
       }, 

       defaultView: 'month', 
       editable: true, 
       events: data, 
       height: 650 
      }); 

     }, 
     error: function() { 

     } 
    }); 


    }) 
    .done(function (data) { 


    }); 

有人能親切地解釋爲什麼事件是不是在我的日曆顯示?

回答

0

我以前遇到過這個問題,並用以下技術解決。因此,只要試試這個

您需要添加可能落在這裏必須

events: $.map(data, function (item, i) { 
      var event = new Object(); 
      event.title = item.EventName; 
      event.start = moment(item.StartDate).utc(); 
      event.end = moment(item.EndDate).utc(); 
      return event; 
}) 

更詳細的說明訪問

why globalization in web.config makes fullcalendar not to render events

+0

非常感謝moment.js。完美地工作。 –

+0

隨時...快樂編碼.. :) –