views:

182

answers:

2

I want to do something like this

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

Unfortunatelly it doesn't work. In server side the value of the model is {object}.

How do I get 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"); 
} 
+1  A: 
$.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.

Sky Sanders
thanks for your answeri'm trying to do something like that--- Javascript ---var json = JSON.stringify({ model: null });$.get('/Controller/Action/', json, function (data) { });--- Controller ---[HttpGet]public ActionResult Test(object model) { // something return PartialView("TestPartial");}--and don't work, idk what i'm doing wrongthe result of model of controller keep {object}
Kim Tranjan
Why not rely on native implementations? Every native JavaScript implementation I've seen so far is fully compliant with the json2.js API.
Eli Grey
@Eli - no, you just think that they are ;-O And the problem with bugs in native JSON implementations is that they are always going to be out there forcing you to check for that browser/version in order to make sure that you force an overwrite of the faulty implementation. Browsers do not fade quickly - how long do you think you will have to support FF 3.0-3.6? That's how long you would have to use your own JSON.
Sky Sanders
Care to provide an example of a browser with a faulty native JSON implementation, including said fault?
Eli Grey
@Eli - http://stackoverflow.com/questions/2343820/have-you-found-any-bugs-in-native-json-implementations
Sky Sanders
Thanks. I was skeptical at first but I guess I was wrong.
Eli Grey
+2  A: 

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.

EDIT I 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.

Alastair Pitts
thanks for your answer...$.get('/Controller/Action/', null, function(data) {});it doesn't work cuz the return of model keep {object}
Kim Tranjan
can you add your action signature to your original question?
Alastair Pitts
done dude, check it out please
Kim Tranjan
Ok, after a quick test I can see what the issue is and I can't seem to see a way around it. It could be a bug in the default model binder but I doubt it.If you know the type of the model being passed back, strongly type it, it should fix this issue. Apologies I don't have a resolution for you.
Alastair Pitts
Ok Alastair, thanks a lot anyway :)
Kim Tranjan
@Kim Tanjan, I think I've fixed it, see my updated answer.
Alastair Pitts
@Alastair Pitts, I tried what you said and works with parameter of type string, so the parameter is object then keep receiving null...I think that is a bug of the model binder as you said... I'm trying to solve this issue and if I reach the goal I'll keep you aware.. thanks Alastair
Kim Tranjan
@Kim, Doh! I was hoping we'd managed to snare it :( Good luck!
Alastair Pitts