Return single int to ajax/json [closed]

I have a jQuery script that makes an ajax post with some data to an MVC controller.

The controller action does some work and I end up with a single int (total). I want to be able to pass that single value back to the ajax call in the success section and use it...

The problem is I don't know the correct return x to use in the mvc controller and /or how to access it in jQuery.

The ways I have tried all return an object { Total : 123 }


In the MVC controller, you can return return plain "content" which returns what you specify exactly as-is, eg:

var total = 123;
return Content(total);

your calling code will get simply "123".

In the $.ajax call, you can access this such as

success: function(data) { var total = data; }

Alternatively, you can return from the controller as a Json object, eg

return Json(new { Total = total });

and access that via a property:

success: function(data) { var total = data.Total; }

note that it is case-sensitive (hence Total = matches data.Total)