我的控制器中有以下方法
[HttpPost]
public ActionResult GetEventsForMonth(int month, int year)
{
var events = new ListWithDuplicates();
Dictionary<string, string> eventsnew = new Dictionary<string, string>();
var ndates = (from nd in MCBDM.News where nd.nsdate.Month==month && nd.nsdate.Year==year select nd.nsdate).ToList();
var nevents = (from ne in MCBDM.News where ne.nsdate.Month==month && ne.nsdate.Year==year select ne.nbrief).ToList();
for (int i = 0; i < ndates.Count;i++ )
{
events.Add(ndates[i].Day.ToString(), nevents[i].ToString());
eventsnew.Add(ndates[i].Day.ToString(), nevents[i].ToString());
}
Thread.Sleep(1000);
return Json(events);
}
public class ListWithDuplicates : List<KeyValuePair<string, string>>
{
public void Add(string key, string value)
{
var element = new KeyValuePair<string, string>(key, value);
this.Add(element);
}
}
我的_calendar.cshtml文件中有以下代码
<div id="datepicker">
</div>
<div class="row modal-line pad-left20 error-text" id="cal_error">
</div>
<script type="text/javascript">
$(function () {
$("#datepicker").datepicker({
beforeShowDay: function (date) {
var day = date.getDate();
if (day in calendarEvents) {
if (calendarEvents[day] == undefined) {
return [true, 'isActive'];
}
return [true, 'isActive', calendarEvents[day]];
}
return [false, ''];
},
onChangeMonthYear: function (year, month, inst) {
getEvents(month, year);
}
});
var calendarDate = $("#datepicker").datepicker("getDate");
getEvents(calendarDate.getMonth() + 1, calendarDate.getFullYear());
});
</script>
这是一个单独的js文件
var calendarEvents = {};
function getEvents(month,year)
{
$.ajax({
url: "Home/GetEventsForMonth",
type:"Post",
data: JSON.stringify({ month: month, year: year }),
dataType: 'json',
processdata: false,
contentType: 'application/json; charset=utf-8',
success: function (data) {
calendarEvents = data; },
error: function (xhr, ajaxOptions, thrownError) {
$("#cal_error").text("Something Went wrong!! Please try after sometime!!");
},
complete: function (x, y) {
$("#datepicker").datepicker("refresh"); }
});
}
我面临的问题是 - 每当我传递ListWithDuplicates
类型的事件时,我会收到javascript错误说JavaScript runtime error: Object doesn't support property or method 'replace' in jquery-ui.js
但是如果我通过控制器的JSON方法传递eventsnew,我赢了# 39; t得到任何错误,日历将正确显示。任何人都可以告诉我使用的类型有什么问题。我附上了一个手表的截图,其中既有事件又有事件新内容,两者都有相同类型的格式和内容。但是当返回事件时仍然会出现javascript错误。!
答案 0 :(得分:0)
我刚刚将下面的代码添加到我的ajax成功方法中,这就像魅力......:)
问题是eventnew返回一个对象,事件正在返回对象数组作为结果。我刚开始检索对象数组并将其存储为对象..
success: function (data) {
$.each(data, function (index) {
var obj = data[index];
calendarEvents[obj.Key] = obj.Value;
});
},