vb.net FullCalendar Json 事件未通过函数加载
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21582945/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
FullCalendar Json Events not loading through function
提问by user2859240
I am using the fullcalendar control in a vb.net web project. If I hardcode the events: property on the full calendar to a string it loads correctly but I can't get data to load through a POST function.
我在 vb.net web 项目中使用 fullcalendar 控件。如果我将完整日历上的 events: 属性硬编码为一个字符串,它会正确加载,但我无法通过 POST 函数加载数据。
This is the hardcoded value to use a string from the page
这是使用页面中的字符串的硬编码值
events: [<%=eventstring %>]
This is the test value I hardcoded in the code behind.
这是我在后面的代码中硬编码的测试值。
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 }"
That correctly displays the activity on the calendar. If I try the following I do not get the activity to load on the calendar.
这正确地显示了日历上的活动。如果我尝试以下操作,我不会在日历上加载活动。
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",
});
},
This is the function in the codebehind. It returns the exact same string.
这是代码隐藏中的函数。它返回完全相同的字符串。
<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
I have put a breakpoint in the LoadCalendarEvents function and can see that it is getting called when the date on the calendar changes and is returing the string.
我在 LoadCalendarEvents 函数中放置了一个断点,可以看到它在日历上的日期更改并返回字符串时被调用。
What am I missing? Why does the event not display on the calendar when using the POST function?
我错过了什么?为什么使用POST功能时日历上不显示事件?
采纳答案by user2859240
I was able to get fullcalendar to load the events successfully by having the webmethod return an array instead of a json string.
通过让 webmethod 返回一个数组而不是一个 json 字符串,我能够让 fullcalendar 成功加载事件。
This is how I setup the eventsources function in javascript. I removed additional fields from the success function and a couple of other functions to load events from different sources to shorten the answer.
这就是我在 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)
],
The webmethod is basically layed out like below. I have just removed some additional logic that doesn't relate to the question. You could probably skip the while loop and just use a list of (your class).ToArray() but I needed the while for additional functionality I have removed from this code.
webmethod 基本上布局如下。我刚刚删除了一些与问题无关的附加逻辑。您可能可以跳过 while 循环,只使用 (您的类).ToArray() 的列表,但我需要使用 while 来实现我从这段代码中删除的附加功能。
<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
回答by MarCrazyness
Checkout the fullcalendar docs: http://arshaw.com/fullcalendar/docs/event_data/events_json_feed/
查看 fullcalendar 文档:http://arshaw.com/fullcalendar/docs/event_data/events_json_feed/
From documentation:
从文档:
$('#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...
]
});

