In my ASP.NET MVC app, I've got this AJAX call in my View's script section:
$(".ckbx").change(function () {
. . .
$.ajax({
type: 'GET',
url: '@Url.Action("GetUnitReportPairVals", "Home")',
data: { unit: unitval, report: rptval },
cache: false,
success: function (result) {
alert(result);
}
});
});
Stepping through the Controller action being called:
public ActionResult GetUnitReportPairVals(string unit, string report)
{
HomeModel model = new HomeModel();
int rptId = GetReportIDForName(report);
DataTable UnitReportPairEmailValsDT = new DataTable();
UnitReportPairEmailValsDT = SQL.ExecuteSQLReturnDataTable(
SQL.UnitReportPairEmailQuery,
CommandType.Text,
new SqlParameter()
{
ParameterName = "@Unit",
SqlDbType = SqlDbType.VarChar,
Value = unit
},
new SqlParameter()
{
ParameterName = "@RptID",
SqlDbType = SqlDbType.Int,
Value = rptId
}
);
model.UnitReportPairEmailVals = UnitReportPairEmailValsDT;
return View(model);
}
...it all seems to be working fine; "model" contains the expected value in UnitReportPairEmailVals.
But I never see the alert message from here in the Ajax call:
success: function (result) {
alert(result);
}
So why is the "success" function of the jQuery AJAX call not being reached?
UPDATE
BTW, this may be a clue: the final line of my Controller method:
return View(model);
...paints the word "View" red as a Rhode Island rooster's comb. But if it's not supposed to be that, what is it supposed to be? That's the exact same thing I have at the end of my "public ActionResult Index()" controller Action, which works fine.
UPDATE 2
I changed the end of my Ajax call to:
success: function (result) {
alert(result);
},
failure: function (error) {
alert(error);
}
...and the end of my Controller method to:
return Json(new { Result = model }, JsonRequestBehavior.AllowGet);
...but I get no alert, neither from success nor from failure.
UPDATE 3
Note: When I tried this:
var model = JSON.stringify({ unit: unitval, report: rptval });
$.ajax({
type: 'GET',
url: '@Url.Action("GetUnitReportPairVals", "Home")',
data: model,
contentType: 'application/json',
cache: false,
success: function (result) {
alert(result);
},
error: function (result) {
debugger;
}
});
..."unit" was null, and so it wouldn't fly at all.
When I changed it to this (removed line 1 and changed the "data" arg back to what I had been using previously):
$.ajax({
type: 'GET',
url: '@Url.Action("GetUnitReportPairVals", "Home")',
data: { unit: unitval, report: rptval },
contentType: 'application/json',
cache: false,
success: function (result) {
alert(result);
},
error: function (result) {
alert('failed');
}
});
..."unit" is what I expect it to be (and "report", too), but I ultimately see 'failed'.
UPDATE 4
Once I pieced together several different answers to this and related questions, I wrote up a tip on how to do this here.
UPDATE 5
I've got this [working] ajax call [working]:
var model = JSON.stringify({ unit: unitval, report: 1 });
$.ajax({
type: 'GET',
url: '@Url.Action("GetUnitDataRangeParamsVals", "UnitReportDataRangeParams")',
data: { unit: unitval, report: 1 },
contentType: 'application/json',
cache: false,
success: function (returneddata) {
populatedatarangeprams(1, returneddata);
},
error: function () {
alert('error - returneddata.error.stringify');
}
});
...but I'm not using the "model" that's declared and, according to my understanding, it is paired with "contentType: 'application/json',"
So I thought what I should do is something like this instead:
var model = JSON.stringify({ unit: unitval, report: 1 });
$.ajax({
type: 'GET',
url: '@Url.Action("GetUnitDataRangeParamsVals", "UnitReportDataRangeParams")',
data: model,
contentType: 'application/json',
cache: false,
success: function (returneddata) {
populatedatarangeprams(1, returneddata);
},
error: function () {
alert('error - returneddata.error.stringify');
}
});
Now the data passed to the controller's method is in json stringified format (encapsulated in "model").
But, it doesn't even work.
It works with the first block, but not the second.
So I removed both the "model" and the "contentType" lines, and changed the "data" line back to what it had been, and it works fine. So are they useless, or am I employing them wrongly?