I have an ajax call that passes data for two parameters to a controller action. The action is called, but the parameters for the action always have null values (in this case the integer is its default of value of 0). I have made sure to check the packets sent by the ajax call and I do see the correct values I intend to send with the correct parameter/attribute names in stringified JSON.
Here is the Controller Action:
[HttpPost]
public IActionResult Summary(int courseID, String test)
{
using (LearningCurveContext db = new LearningCurveContext())
{
Course course = db.Courses.Where(c => c.CourseId == courseID).FirstOrDefault();
CourseSummary courseSummary = new CourseSummary
{
courseID = course.CourseId,
courseName = course.Name,
courseDescription = course.Description
};
return PartialView(courseSummary);
}
}
Here is the Ajax call:
$(document).on("click", ".courseName", function ()
{
var element = this;
$.ajax({
url: $(element).attr("data-url"),
type: "POST",
data: JSON.stringify({ "courseID": "1", "test": "blah" }),
dataType: "json",
contentType: "application/json; charset=utf-8",
cache: false,
success: function (data)
{
$("#details-live").html(data);
},
error: function ()
{
alert("Could not load course summary");
}
});
});
I have tried removing the content type option, as well as not stringifying the data and sending the value of courseID as 1 as well as "1". Nothing seems to work. The URL is correct as the action is being called and the code is run -- just the data doesn't seem to be bound.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…