0

I received this error when I ran my calendar ... I already used these methods in another calendar with another table ... and it worked. The ObjectContext instance has been dropped and can no longer be used for operations that require a connection ... reference to the GetEvents method.

Model

public partial class Reserva
{
    public int ID_Reserva { get; set; }
    public int ID_Cliente { get; set; }
    public int ID_Quarto { get; set; }
    public System.DateTime DataEntrada { get; set; }
    public Nullable<System.DateTime> DataSaida { get; set; }
    public int NumeroPessoas { get; set; }
    public Nullable<int> NumeroNoites { get; set; }
    public Nullable<decimal> Preço { get; set; }
    public string Observaçoes { get; set; }

    public virtual Cliente Cliente { get; set; }
    public virtual Quarto Quarto { get; set; }
}

Controller

public JsonResult GetEvents()
{
    using (HotelEntities dc = new HotelEntities())
    {
        var events = dc.Reserva.ToList();
        return new JsonResult { Data = events, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
    }
}

View

 $(document).ready(function () {
            var events = [];
            var selectedEvent = null;
            FetchEventAndRenderCalendar();
            function FetchEventAndRenderCalendar() {
                events = [];
                $.ajax({
                    type: "GET",
                    url: "/CalendárioReservas/GetEvents",
                    success: function (data) {
                        $.each(data, function (i, v) {
                            events.push({
                                eventID: v.ID_Reserva,
                                clienteID: v.ID_Cliente,
                                quartoID: v.ID_Quarto,
                                inicio: moment(v.DataEntrada),
                                fim: v.DataSaida != null ? moment(v.DataSaida) : null,
                                noites: v.NumeroNoites,
                                pessoas: v.NumeroPessoas,
                                preço: v.Preço,
                                obs: v.Observaçoes
                            });
                        })

                        GenerateCalender(events);
                    },
                    error: function (error) {
                        alert('failed');
                    }
                })
            }

....................................

Error Image

  • Do not wrap the code in a `using (HotelEntities dc = new HotelEntities())` –  May 14 '18 at 10:03
  • @StephenMuecke Thanks for your reply ... but the error continues –  May 14 '18 at 10:16
  • `var events = HotelEntities().Reserva.ToList(); return Json(events, JsonRequestBehavior.AllowGet );` –  May 14 '18 at 10:18
  • 1
    But you should be creating a collection of anonymous objects (or use a model) that returns only the data you need in the view) –  May 14 '18 at 10:20
  • I get the error Failed to load resource: the server responded with a status of 500 (Internal Server Error) ---------> in GetEvents –  May 14 '18 at 10:24
  • Presumably because you have a circular reference error (use you browser tools - the Network tab - to inspect the response which will confirm that) - but that will not happen of you follow the instructions in my last comment. –  May 14 '18 at 10:27
  • Now I have this error A circular reference was detected by undoing the serialization of an object of type 'System.Data.Entity.DynamicProxies.Reserve_73D48092CBEBB4B5640E6FC43E85C81FBFD8F2DAFA3EFB5A6AAC3BEFCF6F9351 –  May 14 '18 at 10:38
  • Working !!! was an error in JSON return –  May 14 '18 at 10:46

0 Answers0