我试图在ASP.NET MVC应用程序中使用fullcalendar.来显示事件。我已经加载了一些测试数据,但它没有显示在日历中(日历显示)。我已经在JavaScript中实现了一些错误处理,并且控制器上显示的调用似乎正常工作。
当直接转到控制器URL时,也会有正确的数据。
助手类
public class FCEvents
{
public int ID;
public string EventTitle;
public string StartDateString;
public string EndDateString;
public static List<FCEvents> LoadAllAppointmentsInDateRange()
{
using (EventDb ev = new EventDb())
{
var rslt =
from e in ev.Events
select new EventsListViewModel
{
EventID = e.EventID,
EventTitle = e.EventTitle,
CourseCode = e.Course.CourseCode,
StartDate = e.StartDate,
FinishDate = e.FinishDate,
Location = e.Location
};
List<FCEvents> result = new List<FCEvents>();
foreach (var item in rslt)
{
FCEvents rec = new FCEvents ();
rec.ID = item.EventID;
rec.EventTitle = item.EventTitle + "-" + item.CourseCode + "-" + item.Location;
rec.StartDateString = item.StartDate.ToString("s");
rec.EndDateString = item.FinishDate.ToString("s");
result.Add(rec);
}
return result;
}
}
}
GetEvents控制器
public JsonResult GetEvents()
{
var elc = FCEvents.LoadAllAppointmentsInDateRange();
var EventList =
from e in elc
select new
{
id = e.ID,
title = e.EventTitle,
start = e.StartDateString,
end = e.EndDateString
};
var rows = elc.ToArray();
return Json(rows, JsonRequestBehavior.AllowGet);
}
JavaScript
@section Scripts {
<script type="text/javascript">
$(document).ready(function () {
GetEvents();
});
function GetEvents() {
$('#calendar').fullCalendar({
header: {
left: '',
center: 'title',
right: 'prev, next, today'
},
defaultView: 'month',
editable: false,
selectable: true,
dataType: 'json',
eventSources: [
{
url: '@Url.Action("GetEvents")',
type: 'POST',
backgroundColor: 'red',
success: function () {
alert('we got the events!');
},
error: function () {
alert('there was an error while fetching events!');
},
}],
});
}
</script>
}
这是控制器的结果:
&#34; [{&#34; ID&#34;:1,&#34; EventTitle&#34;:&#34; FIrstAid T- \ r \ nHLTAID001-South Morang&#34;, &#34; StartDateString&#34;:&#34; 2015-08-14T10:00:00&#34;&#34; EndDateString&#34;:&#34; 2015-08-15T00:00:00&#34 ;},{&#34; ID&#34;:3,&#34; EventTitle&#34;:&#34; First Aid F- \ r \ nHLTAID001-Richmond&#34;,&#34; StartDateString&#34; :&#34; 2015-08-11T02:08:00&#34;&#34; EndDateString&#34;:&#34; 2015-08-13T00:00:00&#34;},{&#34; ID&#34;:4,&#34; EventTitle&#34;:&#34; Fire T- \ r \ nHLTAID001-Richmond&#34;,&#34; StartDateString&#34;:&#34; 2015-08- 20T03:08:00&#34;&#34; EndDateString&#34;:&#34; 2015-08-25T00:00:00&#34;},{&#34; ID&#34;:2000,&# 34; EventTitle&#34;:&#34; Testing- \ r \ nHLTAID001-Mill Park&#34;,&#34; StartDateString&#34;:&#34; 2015-08-15T00:00:00&#34;, &#34; EndDateString&#34;:&#34; 2015-08-20T00:00:00&#34;}]&#34;
任何帮助都会受到赞赏,我花了最后两天回到这里,我似乎无法弄明白。
回顾一下,日历有效,但没有显示任何事件。
答案 0 :(得分:0)
我一直在解决相同的问题,所以我想我知道挫折感。但是,我尝试了下面的示例,这是有效的。我没有看到太大的区别,只有@ Url.Action在JavaScript中给我一个错误
eventSources: [
{
url: '/Agenda/GetEvents/',
type: 'POST',
data: { userId: $('#UserId').val() },
backgroundColor: 'red',
success: function () {
alert('we got the events!');
},
error: function () {
alert('there was an error while fetching events!');
},
}
],