JavaScript runtime error: Object doesn't support property or method 'replace' in jquery-ui.js

JavaScript runtime error: Object doesn't support property or method 'replace' in jquery-ui.js

我的控制器中有以下方法

[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 控制器的方法,我不会收到任何错误,日历也会正确显示。谁能告诉我我使用的类型有什么问题。我附上了 watch 的屏幕截图,其中包含 events 和 eventsnew 内容,并且两者的格式和内容类型相同。但是返回事件时仍然出现 javascript 错误。!

我刚刚将以下代码添加到我的 ajax 成功方法中,效果非常好。:)

问题是 eventsnew 正在返回一个对象,而 events 正在返回对象数组作为结果。我刚开始检索对象数组并将其存储为对象..

  success: function (data) {
            $.each(data, function (index) {
                var obj = data[index];
                calendarEvents[obj.Key] = obj.Value;
            });
    },