My aim was to use ajax to retrieve some data from my controller and append this to a table element in my view. However the javascript code only works until "$.ajax(" at which point it just stops. I placed some break points after the above mentioned point but the code never "broke" at those points.
This is the javascript code
$(document).ready(function ()
{
$("#DivisionId").change(function () {
var OptionId = $('#DivisionId').val();
var position = 0;
$.ajax(
{
type: 'POST',
url: '@URL.Action("Division")',
dataType: 'json',
data: { DivisionId: OptionId },
success: function (data) {
var LogStandings = '';
$.each(data, function (i, log) {
position++;
var Logs = "<tr>" +
"<td>" + position + "</td>" +
"<td>" + log.Logs1.Team.TeamName + "</td>" +
"<td>" + log.Logs1.Played + "</td>" +
"<td>" + log.Logs1.Win + "</td>" +
"<td>" + log.Logs1.Draw + "</td>" +
"<td>" + log.Logs1.Lost + "</td>" +
"<td>" + log.Logs1.GoalsFor + "</td>" +
"<td>" + log.Logs1.GoalsAgainst + "</td>" +
"<td>" + log.Logs1.GoalDifference + "</td>" +
"<td>" + log.Logs1.Points + "</td>" +
"</tr>";
$('#TheLog').append(Logs);
});
},
error: function (ex) {
var r = jQuery.parseJSON(response.responseText);
alert("Message: " + r.Message);
alert("StackTrace: " + r.StackTrace);
alert("ExceptionType: " + r.ExceptionType);
}
});
return false;
}); // end of Division on change function
})//End of document.ready function
This is the code in my controller
public JsonResult Division(int DivisionId)
{
int UnderId = db.Under.FirstOrDefault().UnderID;
MainDivisionId = id;
List<FixtureModel> Fixtures = db.Fixtures.Where(f => f.TeamModel.DivisionId == id && f.TeamModel.UnderId == UnderId).ToList();
List<ResultModel> Results = db.Results.Where(r => r.Fixtures.TeamModel.DivisionId == id && r.Fixtures.TeamModel.UnderId == UnderId).ToList();
List<LogModel> Logs = db.Logs.Where(l => l.Team.DivisionId == id && l.Team.UnderId == UnderId).ToList();
IndexViewModel IndexPage = new IndexViewModel(Fixtures, Results, Logs);
return Json(IndexPage , JsonRequestBehavior.AllowGet);
}
I searched a little on the internet and one source suggested that I place my jquery CDN at the top of my view within the head tag, this did not solve my problem though. Besides that I have not been able to find any other solutions after an hour of searching.
[HttpPost]attribute on your controller method? Also, where exactly were the breakpoints you set? Were they for example on thereturn false? Or in thesuccessorerrorcallbacks?JSON.stringifyto your Data