I’m trying to pass data to an MVC post action that is expecting 1 integer parameter.
[HttpPost]
public ActionResult DeactivateDepartment(int departmentId)
{
return Json(new
{
success = _departmentsRepository.ToggleDepartmentState(departmentId, false)
});
}
The jquery that I’m trying to utilize to hit this action looks like this…
var departmentId = 1;
$.ajax({
url: '/Manage/DeactivateDepartment',
type: 'POST',
dataType: 'json',
success: function (data) {
alert(data);
},
data: departmentId
});
When I take the parameter off of the Action method and remove the data attribute from the jquery ajax object I am successfully able to hit the action method. However, with the action methhod and ajax object as they are shown here I am getting an internal server error.
I’ve been looking at this post.
jQuery AJAX works to return mvc 3 partial view but jQuery load doesn't
It shows how to pass a json object that would be deserialized as an object when it hit the action method, but I don’t want to have to create a custom class just to receive this one integer value that I’m trying to pass to the method. How can I configure this thing to just accept the one value?
Try