0

Following this tutorial and my question, now I can't get the events from my database. The model:

public class Event
{
    public virtual int id { get; set; }
    public virtual string title { get; set; }
    public virtual DateTime start { get; set; }
    public virtual DateTime end { get; set; }
}

The controller:

public class EventController : ApiController
{

    private SGPContext db = new SGPContext();

    // GET api/event
    public IQueryable GetEvents()
    {
        return db.Events;
    }

    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            db.Dispose();
        }
        base.Dispose(disposing);
    }

}

And the view:

<div id="fullcalendar"></div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")

    <script type="text/javascript">
        $(function () {
            $('#fullcalendar').fullCalendar({
                header: {
                    left: 'prev,next today',
                    center: 'title',
                    right: 'month,basicWeek,basicDay'
                },
                defaultDate: new Date(),
                events: "/api/event/"
            });
        });
    </script>
}

I have this in the database:

id:1
title:test
start:25/08/2016 09:00:00
end:25/08/2016 13:00:00

I already saw some questions and answers about not showing the content of the database but I think everything I saw was a bit different from what I have. What's missing in my code?

Community
  • 1
  • 1
dasdasd
  • 97
  • 1
  • 14
  • check in console what's actually returning from your controller. I mean `F12` i browser. and watch what's you get from `/api/event/` GET request. I suppose it's not what you expect to get. – teo van kot Aug 24 '16 at 10:39
  • @teovankot i've done it and its not getting anything i guess, but in the tutorial thats how it is... i'm a newbie, i tried to change `/api/event/` to a lot of other links but it didn't work – dasdasd Aug 24 '16 at 11:14
  • @dasdasd have you tried just "api/event" without the leading slash? If it's hitting the wrong link you'll probably get a HTTP error back - 404, or maybe something else if the server's badly configured. – ADyson Aug 24 '16 at 11:16
  • add debug point to `return db.Events;` and find link that allows you to stop on this debug point – teo van kot Aug 24 '16 at 11:16
  • @ADyson it didn't give any error – dasdasd Aug 24 '16 at 11:29
  • @teovankot it gets what's in the database correctly – dasdasd Aug 24 '16 at 11:31
  • @dasdasd so the calendar is just blank then? If you go to the F12 Developer Tools, click on the Network tab, click on the request to your "api/event" method, and then click on the "response" section, you should see the exact JSON that's returned. (These instructions are for Chrome, other browser tool might be organised slightly differently). Then you can verify that the JSON is in the format required by fullCalendar – ADyson Aug 24 '16 at 13:30
  • @ADyson I'm using firefox and the status of the request to "api/event" is 500 Internal Server Error and it doesn't show any JSON, only this message: The 'ObjectContent`1' type failed to serialize the response body for content type 'application/json; charset=utf-8'. – dasdasd Aug 24 '16 at 13:52
  • 1
    @dasdasd so it _did_ give an error, contrary to your earlier statement :-). Basically it's saying it can't convert your Events object into JSON. Maybe one of the solutions here will help. http://stackoverflow.com/questions/12641386/failed-to-serialize-the-response-in-web-api It's not a problem I've ever encountered personally. – ADyson Aug 24 '16 at 14:00
  • @ADyson I followed the answer in that question and it works, thanks for your help – dasdasd Aug 24 '16 at 14:05

0 Answers0