在MVC.Net中必须为JsonResult传递什么样的对象

时间:2015-07-28 14:29:15

标签: asp.net-mvc

所以我将一个自定义类传递给我的控制器,似乎没有正确传递JsonResult。

困扰我的是(也是完整日历不会读取json)我在视图中的console.log打印了函数的路径(wtf?)而不是Json应该返回的

这是我的代码:

    public JsonResult GetCalendarEvents()
    {
        var eventList = BusinessLayer.Event.getAllEvents();

        return Json(eventList.ToArray(), JsonRequestBehavior.AllowGet);
    }

为了实现这个目的,必须传递什么样的对象?

我的evenList类型为List<Event>

    public static String ListToString(List<Event> evs)
    {
        String ret = "";
        foreach (var ev in evs)
        {
            ret += ev.ToString() + "\n";
        }
        return ret;
    }

    public static List<Event> getAllEvents()
    {
        List<DataLayer.Event> dbEvents = DataApi.db.Event.ToList();
        List<Event> returnEvents = new List<Event>();
        foreach (DataLayer.Event oneEvent in dbEvents)
        {
            Event newEvent = new Event
            {
                ID = oneEvent.IDEvent,
                userID = oneEvent.UserID,
                projectID = oneEvent.ProjectID,
                jobtypeID = oneEvent.JobTypeID,
                taskID = oneEvent.TaskID,
                ticketID = oneEvent.TicketID,
                loccoID = oneEvent.LoccoID,
                startTime = oneEvent.StartTime,
                endTime = oneEvent.EndTime,
                shiftFrom = oneEvent.ShiftFrom,
                shiftTo = oneEvent.ShiftTo,
                description = oneEvent.Description,
                billable = oneEvent.Billable
            };
            returnEvents.Add(newEvent);
        }
        return returnEvents;
    }

我尝试在fullcalendar中显示事件:

$('#calendar').fullCalendar({
    header: {
        left: 'title',
        center: '',
        right: 'prev,next today basicDay,basicWeek,month',
    },

    //events: "/Calendar/GetEvents/", // not implemented
    events: "@Url.Action("GetCalendarEvents/")",

并将结果输出到控制台:

console.log("@Url.Action("GetCalendarEvents/")");

但我明白了:

VM84 Index:83 /Calendar/GetCalendarEvents/

fullcalendar.min.js:6 Uncaught TypeError: Cannot read property 'hasTime' of undefined

1 个答案:

答案 0 :(得分:1)

看起来你错过了一些必填字段。如果您查看documentationtitlestart 必需。尝试在课堂上设置这些,然后从...开始构建......

public static List<Event> getAllEvents()
{
    List<DataLayer.Event> dbEvents = DataApi.db.Event.ToList();
    List<Event> returnEvents = new List<Event>();
    foreach (DataLayer.Event oneEvent in dbEvents)
    {
        Event newEvent = new Event
        {
            start = oneEvent.StartTime,
            title = oneEvent.Description // you may need to add this to your Event class.
        };
        returnEvents.Add(newEvent);
    }
    return returnEvents;
}

此外,使用Fiddler或Chrome高级工具

,而不是使用 console 来记录 Json