List of My Models:
ClassAllocate: Contains Id, DepartmentId, CourseId, RoomId, DayId, StartTime, EndTime
Course: Consists of Id, CourseCode, CourseName, DepartmentId
Room: Includes Id, RoomNumber
Day: Has Id and DayName
My goal is to search for courses by department ID from the "ClassAllocates" table on the view page and display only the schedule/allocation details for those courses.
I am utilizing JSON to send lists from the controller to the view. I am facing difficulty in sending multiple lists from a JsonResult function. Although I can send them (tried with 2 lists), I am unable to display them properly - it either shows [object Object], nothing, or undefined in my attempts.
Below, I am sharing my Controller function and JavaScript code in the view:
1st: Controller Function
public JsonResult GetCourseIdListByDepartmentId(int departmentId)
{
var x = db.ClassAllocates.DistinctBy(m => m.CourseId).Where(m => m.DepartmentId == departmentId).ToList();
var r = db.ClassAllocates.DistinctBy(m => m.RoomId).Where(m => m.DepartmentId == departmentId).ToList();
var all = new [] {x,r}.ToList();
return Json(all, JsonRequestBehavior.AllowGet);
}
2nd: View JavaScript
<script>
$("#DepartmentId").change(function () {
var dptId = $("#DepartmentId").val();
//alert(dptId);
$(".RowClass").empty();
var json = {
departmentId: dptId
};
$.ajax({
type: "POST",
url: '@Url.Action("GetCourseIdListByDepartmentId", "ClassAllocates")',
contentType: "application/json; charset=utf-8",
data: JSON.stringify(json),
success: function (data) {
$.each(data, function (key, value) {
$(".table2").append(
'<tr class="RowClass">' +
'<td>' + value.Couse + '</td>'
+ '<tr>');
});
}
});
});
The current output displays as: undefined
I specifically require CourseCode, CourseName, and RoomNumber to be displayed.