I have the following controller action:
[HttpPost]
public ActionResult GetCourseSections(int courseID)
{
var Sections = dbcontext.CourseSection.Where(cs => cs.CourseID.Equals(courseID)).Select(x => new
{
sectionID = x.CourseSectionID,
sectionTitle = x.Title
});
return Json(Sections, JsonRequestBehavior.AllowGet);
}
This returns the list that I am expecting. I then try to retrieve this list to populate a dropdown using the following code:
$.getJSON('@Url.Action("GetCourseSections", "Admin")',
function(data) {
var courseSectionDropdown = $('#coursesectiondd');
courseSectionDropdown.empty();
courseSectionDropdown.append($('<option/>', {
value: 0,
text: "Test"
}));
$.each(data, function (index, data) {
courseSectionDropdown.append($('<option/>', {
value: data.value,
text: data.text
}));
});
});
Although on debug I am able to see the list of JSON objects when adding them the only list item being added is the default option "Test" that I am setting. Can anyone see from my code why the data is not being read? Have I made a mistake in the jquery