Как исправить внутреннюю ошибку 500 сервера на ajax перезвоните в asp.net mvc
Я буквально все перепробовал, но не могу найти информацию о переадресации ajax, ответ всегда переходит к атрибуту ошибки ajax, но безуспешно
jquery-1.9.1.min.js: 5 POST http://localhost:60854/ExamController/AjaxMethodForDepartment 500 (внутренняя ошибка сервера)
HTML-код:
<!DOCTYPE html>
<link href="~/Content/css/bootstrap.min.css" rel="stylesheet" />
<link href="~/Content/css/bootstrap-multiselect.css" rel="stylesheet" />
<script src="~/Scripts/jquery-1.9.1.min.js"></script>
<script src="~/Scripts/bootstrap.min.js"></script>
<script src="~/Scripts/bootstrap-multiselect.js"></script>
<html>
<head>
<title>CreateExam</title>
</head>
<body>
<h2>Department</h2>
<select id="dep" multiple="multiple">
</select>
<h2>Batch</h2>
<select id="batch" multiple="multiple">
<option disabled selected>-No Department Selected-</option>
</select>
<form action="/ExamController/CreateExam" method="post">
<h2>Subject</h2>
<select id="subject" multiple="multiple" name="subjects">
<option disabled selected>-No Batch Selected-</option>
</select><br><br>
<input type="submit" />
</form>
</body>
</html>
$(function () {
$("select").multiselect({
includeSelectAllOption: true
});
$(document).ready(function () {
$.ajax({
type: "POST",
url: "/ExamController/AjaxMethodForDepartment",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
len = response.length;
if (len == 0) {
$("#dep").html('<option disabled selected>-There are no Departments in DB-</option>');
$("#batch").html('<option disabled selected>-No Department Selected-</option>');
$("#dep,#batch").multiselect('rebuild');
}
else {
for (var i = 0; i < len; i++) {
if (i == 0)
$("#dep").html(new Option(response[i].Department_Id, response[i].Department_Id));
else
$("#dep").append(new Option(response[i].Department_Id, response[i].Department_Id));
}
$("#dep").multiselect('rebuild');
}
},
error: function (jqXHR, textStatus, errorThrown) {
alert("error: " + errorThrown);
$("#dep").html('<option disabled selected>-There are no record of Departments in DB-</option>');
$("#batch").html('<option disabled selected>-No Department Selected-</option>');
$("#dep,#batch").multiselect('rebuild');
}
});
});
$("#dep").change(function () {
var len;
$.ajax({
type: "POST",
url: "/ExamController/AjaxMethodForBatch",
data: JSON.stringify({ departments: $("#dep").val() }),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
len = response.length;
if (len == 0) {
$("#batch").html('<option disabled selected>-Selected Department does not have any Batch-</option>');
$("#subject").html('<option disabled selected>-No Batch Selected-</option>');
$("#batch,#subject").multiselect('rebuild');
}
else {
for (var i = 0; i < len; i++) {
if (i == 0)
$("#batch").html(new Option(response[i].Batch_Id, response[i].Batch_Id));
else
$("#batch").append(new Option(response[i].Batch_Id, response[i].Batch_Id));
}
$("#batch").multiselect('rebuild');
}
},
error: function (response) {
alert("Batch Error!");
$("#batch").html('<option disabled selected>-No Department Selected-</option>');
$("#subject").html('<option disabled selected>-No Batch Selected-</option>');
$("#batch,#subject").multiselect('rebuild'); }
});
});
$("#batch").change(function () {
var len;
$.ajax({
type: "POST",
url: "/ExamController/AjaxMethodForSubject",
data: JSON.stringify({ batches: $("#batch").val() }),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
len = response.length;
if (len == 0) {
$("#subject").html('<option disabled selected>-Selected Batch does not have any Subjects-</option>');
$("#subject").multiselect('rebuild');
}
else {
for (var i = 0; i < len; i++) {
if (i == 0)
$("#subject").html(new Option("Subject Id: " + response[i].Subject_Id + ", Subject Name: " + response[i].Subject_Name , response[i].Subject_Id));
else
$("#subject").append(new Option("Subject Id: " +response[i].Subject_Id + ", Subject Name: " + response[i].Subject_Name , response[i].Subject_Id));
}
$("#subject").multiselect('rebuild');
}
},
error: function (response) {
alert("Subject Error!");
$("#subject").html('<option disabled selected>-No Batch Selected-</option>');
$("#subject").multiselect('rebuild');
}
});
});
});
контроллер:
public class ExamControllerController : Controller
{
//
// GET: /ExamController/
FYP_DB_Entities obj = new FYP_DB_Entities();
public ActionResult Index()
{
return View();
}
public ActionResult CreateExam()
{
return View(obj.Departments.ToList());
}
[HttpPost]
public ActionResult ExamCreated(int[] subjects)
{
Subject s = new Subject();
Exam e = new Exam();
Batch b = new Batch();
foreach (var i in subjects)
{
s = obj.Subjects.First(x => x.Subject_Id == i);
b = obj.Batches.First(x => x.Batch_Id == s.Batch_Id);
e.Subject_Id = s.Subject_Id;
e.Batch_Id = s.Batch_Id;
e.Department_Id = b.Department_Id;
e.Total_Marks = 50;
e.Exam_Session = "Fall-2017";
e.Status = "Inactive";
obj.Exams.Add(e);
obj.SaveChanges();
}
return View();
}
[HttpPost]
public JsonResult AjaxMethodForDepartment()
{
IEnumerable<Department> departmentList = Enumerable.Empty<Department>();
try
{
departmentList = obj.Departments.ToList();
return Json(departmentList);
}
catch
{
return Json(null);
}
}
[HttpPost]
public JsonResult AjaxMethodForBatch(string[] departments)
{
IEnumerable<Batch> batchList = Enumerable.Empty<Batch>();
try
{
batchList = departments.SelectMany(d => obj.Batches.Where(x => x.Department_Id == d));
return Json(batchList);
}
catch
{
return Json(null);
}
}
[HttpPost]
public JsonResult AjaxMethodForSubject(string[] batches)
{
IEnumerable<Subject> subjectList = Enumerable.Empty<Subject>();
try
{
subjectList = batches.SelectMany(d => obj.Subjects.Where(x => x.Batch_Id == d));
return Json(subjectList);
}
catch
{
return Json(null);
}
}
скриншот вкладки сети Chrome