user3402743 user3402743 - 1 month ago 5x
C# Question

Parameters not passing into an ActionResult

So I have this Javascript code:

function AddToStuff(somethingID) {
var stuffID = document.getElementById('StuffID').innerText;

alert("First Alert: " + stuffID);

if (stuffID == -1) {
document.getElementById('StuffID').innerText = 0;
stuffID = 0;

alert("Second Alert: " + stuffID), stuffID);

$(document).ready(function () {

var StuffCallback = $.Callbacks();

function initializeStuff() {
StuffCallback.add(function (somethingID, stuffID) {
url: "pos/Stuff",
type: "GET",
traditional: true,
data: {
somethingID: somethingID,
stuffID: stuffID

success: function (result, textStatus, xhr) {
alert("Third Alert: " + stuffID);
var contentArea = $("#Stuff");

$("#Stuff").show({ duration: 250 });

And this C# code:

public ActionResult Stuff(int somethingID = 0, int stuffID = -1)
if (stuffID == -1)
//do something
else if (stuffID == 0)
//do something else
//do something else

return View();

The problem is that the C# method always has stuffID at -1 and so performs the first "if" case.

When the page loads and $(document).ready calls initializeStuff, the stuffID is -1, so it performs the first "if" case, as desired. Later, when AddToStuff() is called, because the user clicked on something, it reads in my element StuffID, which I have initialized to -1. The First Alert does display the -1. Then StuffID gets changed to 0 in the "if" inside AddToStuff() and the Second Alert does display the 0.

But then I have the Javascript function AddToStuff call the C# function again, but the goal is to have the second parameter (stuffID) be the 0 instead of the -1. And even the Third Alert will display a 0. But in the C# function, the stuffID is still -1.

I can't pass in the 0. Any help would be appreciated.

Note: I generalized my code into "something" and "stuff", but that part should be working fine - the functions call when I expect them to.



  • Use fiddler or developer tools in your browser to see what is being passed to the server (0, or -1)?
  • The default value to Stuff(int somethingID = 0, int stuffID = -1) can also be causing the problem if the data received by the server is not correct.
  • Set the data type in your Ajax call dataType: 'json'
  • If you are using a GET request, make sure that you have set the data object correctly