I'm new in JQuery and AJAX. I tried to fill a DropDownList using AJAX in ASP.NET MVC 4 and it gives me this error: The ObjectContext instance has been deleted and can not be used for operations that require a connection. and here is my script:
function LoadFlights() {
var $flight = $('#IDFLIGHT');
$flight.empty();
$flight.append($('<option></option>').val('').html('Please Wait...'));
$.ajax({
url: '/Flight/GetFlightList',
type: 'POST',
data: {},
dataType: 'json',
success: function (d) {
$flight.empty();
$flight.append($('<option></option>').val('').html('Select Flight'));
$.each(d, function (i, val) {
$flight.append($('<option></option>').val(val.IDFLIGHT).html(val.DATEFLIGHT));
});
},
error: function () {
}
});
}
And this is the action in the controller Flight I call:
public JsonResult GetFlightList()
{
FlightService flightService = new FlightService();
var all = flightService.GetAll();
return new JsonResult { Data = all, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
}
The variable all has data but it still give me the error mentioned above. Thank you
$.each(dwith$.each(d.Datavar all = flightService.GetAll().ToList();If that is not working, show yourGetAll()method.