我已经搜索了关于这个主题的stackoverflow,并尝试了解决方案,但对我来说并不奏效。即使我重新加载了整个页面,在我终止程序并重新启动它之前,添加的事件都不会显示在日历中。我知道它正在遍历代码,因为我在refetchEvents和rerenderEvents行都遇到了断点。
以下是javascript:
$(document).ready(function () {
var TheCalendar = $('#ODCalendar').fullCalendar({
header:
{
left: 'prev,next today',
center: 'title',
right: 'month,agendaWeek,agendaDay'
},
buttonText: {
today: 'today',
month: 'month',
week: 'week',
day: 'day'
},
events: function (start, end, timezone, callback) {
$.ajax({
url: '/Home/GetCalendarData',
type: "GET",
dataType: "JSON",
success: function (result) {
var events = [];
$.each(result, function (i, data) {
var bColor = "#000000";
var tColor = "#FFFFFF";
switch (data.ReleaseType) {
case '1':
if (data.Action == '1') {
bColor = '#cc9900';
}
else if (data.Action == '2') {
bColor = '#ffd24d';
tColor = "#000000";
}
else {
bColor = '#e6e6e6';
}
break;
case '2':
if (data.Action == '1') {
bColor = '#ff8080';
}
else if (data.Action == '2') {
bColor = '#ffcccc';
tColor = "#000000";
}
else {
bColor = '#e6e6e6';
}
break;
case '3':
if (data.Action == '1') {
bColor = '#0099ff';
}
else if (data.Action == '2') {
bColor = '#ccebff';
tColor = "#000000";
}
else {
bColor = '#e6e6e6';
}
break;
case '4':
if (data.Action == '1') {
bColor = '#2fb62f';
}
else if (data.Action == '2') {
bColor = '#d6f5d6';
tColor = "#000000";
}
else {
bColor = '#e6e6e6';
}
break;
case '99':
bColor = '#cccccc';
break;
default:
bColor = '#999999';
}
events.push(
{
title: data.Title,
description: data.Description,
start: moment(data.Start_Date).format('YYYY-MM-DD HH:mm'),
end: moment(data.End_Date).format('YYYY-MM-DD HH:mm'),
url: '/Home/ShowEditEvent?id=' + data.id + '&Owner=' + data.Owner,
textColor: tColor,
backgroundColor: bColor,
borderColor: "#000066"
});
});
callback(events);
}
});
},
eventRender: function (event, element) {
element.qtip(
{
content: event.description
});
},
editable: true
});
// Refresh each time home page loads
$('#ODCalendar').fullCalendar('refetchEvents');
$('#ODCalendar').fullCalendar('rerenderEvents');
});
数据是在MVC控制器中创建的。我使用HTML.BeginForm代替AJAX。BeginForm,因为它给我带来的麻烦比它的价值还多:
public ActionResult AddEvent(Guid CalendarID = new Guid(), string Title = "",
int Owner = 0, string Description = "", string Start_Date = "", string End_Date = "",
string ReleaseType = "", string ReleaseVersion = "", string Action = "", string Network = "", string CRQ = "",
string Comments = "", string StoreList = "", bool SharedEntry = true)
{
CalendarEntry model = new CalendarEntry();
try
{
using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["CalendarConnection"].ToString()))
{
string query = "Insert Into CalendarEntry (id, CalendarID, Title, Owner, Description, Start_Date, End_Date, ReleaseType, ReleaseVersion, Action, Network, CRQ, Comments, StoreList, SharedEntry)" +
" Values(@id, @CalendarID, @Title, @Owner, @Description, @Start_Date, @End_Date, @ReleaseType, @ReleaseVersion, @Action, @Network, @CRQ, @Comments, @StoreList, @SharedEntry)";
SqlCommand command = new SqlCommand(query, connection);
command.Parameters.AddWithValue("id", id);
command.Parameters.AddWithValue("CalendarID", CalendarID);
command.Parameters.AddWithValue("Title", Title);
command.Parameters.AddWithValue("Owner", Owner);
command.Parameters.AddWithValue("Description", Description);
//command.Parameters.AddWithValue("Start_Date", Start_Date);
//command.Parameters.AddWithValue("End_Date", End_Date);
command.Parameters.AddWithValue("Start_Date", Start_DateDT);
command.Parameters.AddWithValue("End_Date", End_DateDT);
command.Parameters.AddWithValue("ReleaseType", ReleaseType);
command.Parameters.AddWithValue("ReleaseVersion", ReleaseVersion);
command.Parameters.AddWithValue("Action", Action);
command.Parameters.AddWithValue("Network", Network);
command.Parameters.AddWithValue("CRQ", CRQ);
command.Parameters.AddWithValue("Comments", Comments);
command.Parameters.AddWithValue("StoreList", StoreList);
command.Parameters.AddWithValue("SharedEntry", SharedEntry);
connection.Open();
command.ExecuteNonQuery();
}
}
catch
{
}
return Redirect("Index");
}
我要回到日历所在的索引页面,它会通过javascript代码,但不会重新加载。请问我哪里出了问题?
终于找到了答案。事实上,我两天前发现了它,但把代码放错了地方。这次我找对地方了。只需添加一行:
cache: false
在这里:
events: function (start, end, timezone, callback) {
$.ajax({
url: '/Home/GetCalendarData',
type: "GET",
dataType: "JSON",
cache: false,
success: function (result) {
var events = [];
等等。。。
感谢您的帮助。
- John C