How can I send null value? How can I send null value? json json

How can I send null value?


I'm struggling to understand exactly what you asking so I'm going to try and attempt it. :)

If you just want to send no value then just set the data to null

$.get('/Controller/Action/', null, function(data) {});

If you want to have one parameter of the action null then just exclude it from the data array.

Eg, for this action:

[HttpGet]public JsonResult Foo(string bar, string foo){    /*The Magic*/}

you might only want to send a value for bar and not foo, then the jquery would be:

$.get('/Controller/Foo/', { bar: 'It's a bingo! }, function(data) {});

This will result in bar being the passed value and foo being null.

Another thing to note, to avoid JSON Hijacking, ASP.NET MVC 2 is designed for JSON to be passed as from a POST request rather than a GET.

The reason can be found here: http://haacked.com/archive/2009/06/25/json-hijacking.aspx

To make this change, decorate your Action with the [HttpPost] attribute and change your jQuery to:

$.post('/Controller/Foo/', { bar: 'It's a bingo! }, function(data) {});

I hope this helps.

EDITI just thought/discovered something. Instead of passing { model: null } try passing { model: undefined }.I just had a similar issue where I wanted to pass nothing back to a string parameter and 'null' gave a string of "null" where as undefined gave null.


$.get('/Controller/Action/', '{ "model" : null }', function(data) {});

As others have said, you can't let jQuery do your serialization in this case.

You will need to do it manually or with json2.js, which is always my recommendation. I NEVER rely on jQuery or native implementations as there are just too many inconsistencies and bugs.