I would like to know why the function called by AJAX
keeps fail in this scenario? The FillObj()
is supposed to be called when a dropdownlist
selection changes.
<div class="form-group">
@Html.LabelFor(model => model.Module, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.Module, new SelectList(Model.ModuleList, "Value", "Text"),
"- Please select a Module -", new { @onchange = "FillObj()", @class = "form-control" })
</div>
</div>
As you can see I already have the @onchange
set to FillObj()
in the Dropdownlist
code. However, when the selection changes, it keep goes to alert('A error')
instead of success. The following is the AJAX function
:
function FillObj() {
var moduleID = $('#Module').val();
$.ajax({
url: '/Questions/FillObjectives',
type: "GET",
dataType: "JSON",
data: { module: moduleID },
success: function (objectives) {
$("#objective").html(""); // clear before appending new list
$.each(objectives, function (i, Obj) {
$("#objective").append(
$('<option></option>').val(Obj.ObjectiveId).html(Obj.objective));
});
},
error: function () {
alert('A error');
}
});
}
Controller:
public ActionResult FillObjectives(int module)
{
var objectives = db.Objectives.Where(o => o.ModuleId == module);
return Json(objectives, JsonRequestBehavior.AllowGet);
}
EDIT 1:
Ok, I did some debugging and found out that this error message occured whenever the function is called:
Anyways to fix this?