How can I send a null value?

I want to do something like this

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

Unfortunately this will not work. On the server side, the value of the model {object}.

How do i get it null?

EDIT

--- Javascript ---

var json = JSON.stringify({ model: null });
$.get('/Controller/Action/', json, function (data) { }); 

--- Controller ---

[HttpGet]
public ActionResult Test(object model) 
{ 
// here i need model = null but keep returning {object}
return PartialView("TestPartial"); 
} 
+5
source share
2 answers

I am struggling to understand what you are asking, so I will try to try. :)

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

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

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

For example, for this action:

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

bar, foo, jquery :

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

, bar , foo .

: JSON Hijacking, ASP.NET MVC 2 JSON POST, GET.

: http://haacked.com/archive/2009/06/25/json-hijacking.aspx

, [HttpPost] jQuery :

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

, .

/- . { model: null } { model: undefined }. , , "null" "null", undefined null.

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

, jQuery .

json2.js, . jQuery , .

+1

All Articles