I am just beginning learning .NET MVC 4 framework and am having troubles with some basic CRUD operations yielding errors. I have a select element that upon change, sends an Ajax request to my server to find the Course given the id. Here is the ajax call:
$('.courseSelect').change(function () {
$.ajax({
url: "@Url.Action("Find", "Roster")",
data: {
courseId: $('.courseSelect').val()
},
dataType : 'json',
type: "GET",
success: function (data) {
console.log(data);
},
error: function (xhr, err, type) {
console.log(xhr);
console.log(err);
console.log(type);
}
});
});
This correctly send a request in the form of http://localhost:62020/Roster/Find?courseId=2
I then have a Find ActionRequest in my Roster controller:
[HttpGet]
public ActionResult Find(int? courseId)
{
StudentsViewModel selectedCourse = new StudentsViewModel();
List<Course> courses = Db.Courses.Where(s => s.Id >= 0).ToList();
foreach (Course c in courses)
{
selectedCourse.AllCourses.Add(c);
}
selectedCourse.currentCourse = Db.Courses.Find(courseId);
selectedCourse.AllStudents = Db.Students.Where(s => s.Id >= 0).ToList();
return View(selectedCourse);
}
However, even though the controller logic seems correct when doing a run-time debug of each line, the Ajax fails with a 500 Internal Server Error.