How can I send null value?
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");
}
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.
$.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.
加载中,请稍侯......
精彩评论