ditoslav
ditoslav

Reputation: 4872

What kind of object has to be passed for JsonResult in MVC.Net

So I'm passing a custom class to my controller and it seems that the JsonResult is not properly passed.

What bothers me is that (also the fullcalendar wont read the json) the console.log which I have in my view prints the path to the function (wtf?) instead of what Json shoul return

This is my code:

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

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

What kind of object has to be passed for this to work?

My evenList is of type List<Event> from here:

    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;
    }

I tried displaying the events in fullcalendar:

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

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

and outputing the result to console:

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

but I get:

VM84 Index:83 /Calendar/GetCalendarEvents/

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

Upvotes: 1

Views: 77

Answers (1)

Christian Phillips
Christian Phillips

Reputation: 18769

It looks like you're missing some required fields. If you look at the documentation, title, start are required. Try setting these in the class to start with and build from that...

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;
}

Also, instead of using console to log the Json, use Fiddler or Chrome Advanced Tools

Upvotes: 1

Related Questions