2014-02-05 34 views
0

我在vb.net web項目中使用fullcalendar控件。如果我將完整日曆上的events:property硬編碼爲字符串,則它會正確加載,但無法通過POST函數加載數據。FullCalendar Json事件不通過函數加載

這是使用字符串從頁面

events: [<%=eventstring %>] 

這是測試值,我在代碼中硬編碼背後的硬編碼值。

eventstring = "{ title: 'Testing - This is a test', start:'2014-02-05T19:10:00-04:00' , end: '2014-02-05T19:10:00-03:00', allDay : false }" 

正確顯示日曆上的活動。如果我嘗試以下操作,我不會在日曆上加載該活動。

events: function (start, end, callback) { 
    $.ajax({ 
     type: "POST", 
     url: "calendar.aspx/LoadCalendarEvents", 
     data: '{startDate: "' + $.fullCalendar.formatDate(start, 'M/d/yyyy') + '",' + 'endDate: "' + $.fullCalendar.formatDate(end, 'M/d/yyyy') + '" }', 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 

    }); 
}, 

這是codebehind中的函數。它返回完全相同的字符串。

<System.Web.Services.WebMethod()> _ 
Public Shared Function LoadCalendarEvents(ByVal startDate As String, ByVal endDate As String) As String 
    Dim eventstring As String = "" 

    eventstring = "{ title: 'Testing - This is a test', start:'2014-02-05T19:10:00-04:00' , end: '2014-02-05T19:11:00-03:00', allDay : false }" 

    Return eventstring 
End Function 

我已經把斷點在LoadCalendarEvents功能,可以看到,它獲取調用時,在日曆上更改的日期,並returing的字符串。

我錯過了什麼?爲什麼使用POST函數時事件不會顯示在日曆上?

+0

你能告訴我們你是如何解決你的問題的嗎? – Yuri

+0

我已經添加了我如何解決問題的接受答案。我試圖刪除不必要的代碼來簡化答案。讓我知道你是否需要任何澄清。 – user2859240

回答

0

我能夠具有的WebMethod返回一個數組,而不是一個JSON字符串來獲取fullcalendar成功加載事件。

這是我如何在javascript中設置eventsources函數。我從成功函數和其他一些函數中刪除了額外的字段,以加載來自不同來源的事件以縮短答案。

eventSources: [ 
       // Load Standard Events For Employee 
       function (start, end, callback) { 
        $.ajax({ 
         type: "POST", 
         url: "/calendar.aspx/LoadCalendarEvents", 
         data: '{startDate: "' + $.fullCalendar.formatDate(start, 'M/d/yyyy') + '",' + 'endDate: "' + $.fullCalendar.formatDate(end, 'M/d/yyyy') + ' " , employeeID: "' + $('#lstEmployeesMaster').val() + '"  }', 
         contentType: "application/json; charset=utf-8", 
         dataType: "json", 

         success: function (eventstring) { 
          var buildingEvents = $.map(eventstring.d, function (item) { 
           return { 
            id: item.EventID, 
            title: item.Title, 
            start: item.StartDate, 
            end: item.EndDate, 
            allDay: false, 
            //...(More Fields) 
           }; 
          }); 

          callback(buildingEvents); 
         }, 
         error: function (data) { 
          //alert(data); 
         } 
        }); 
       }, 
       //...(Additional Functions for other events) 
      ], 

webmethod基本上像下面一樣佈局。我剛剛刪除了一些與問題無關的附加邏輯。你可以跳過while循環,只是使用(你的類).ToArray()的列表,但我需要一段時間來從這個代碼中移除額外的功能。

<System.Web.Services.WebMethod()> _ 
Public Shared Function LoadCalendarEvents(ByVal startDate As String, ByVal endDate As String, ByVal employeeID As String) As Array 
    Dim events As DataSet 

    If employeeID > 0 Then ' Individual Employee 
     events = dbEvents.ListByEmployeeForNewCalendar(employeeID, startDate, endDate) 
    Else ' Office 
     events = dbEvents.ListByOfficeForNewCalendar(-1 * employeeID, startDate, endDate) 
    End If 

    Dim eventList As New ArrayList() 

    Dim i As Int32 = 0 
    While i < events.Tables(0).Rows.Count ' Load the details for each event in the range 
     Dim startonDate As DateTime = CDate(events.Tables(0).Rows(i).Item(14)) 
     Dim endonDate As DateTime = CDate(events.Tables(0).Rows(i).Item(15)) 

     Dim eventRecord As New CalendarEventDetail ' This is a simple class with properties for the event detail 
     eventRecord.StartDate = Format(startonDate, "yyyy-MM-dd HH:mm:ss") 
     eventRecord.EndDate = Format(endonDate, "yyyy-MM-dd HH:mm:ss") 
     eventRecord.EventID = events.Tables(0).Rows(i).Item(0).ToString() 
     ' Additional stuff removed 

     eventList.Add(eventRecord) 

     i += 1 
    End While 

    Return eventList.ToArray 
End Function 
0

結帳的fullcalendar文檔: http://arshaw.com/fullcalendar/docs/event_data/events_json_feed/

從技術文檔:

$('#calendar').fullCalendar({ 

    eventSources: [ 

     // your event source 
     { 
      url: '/myfeed.php', 
      type: 'POST', 
      data: { 
       custom_param1: 'something', 
       custom_param2: 'somethingelse' 
      }, 
      error: function() { 
       alert('there was an error while fetching events!'); 
      }, 
      color: 'yellow', // a non-ajax option 
      textColor: 'black' // a non-ajax option 
     } 

     // any other sources... 

    ] 

}); 
相關問題