admin管理员组文章数量:1247209
I have something wrong with either json or ASP.NET MVC, I am using ASP.NET MVC and here is what I am sending from the client.
NOTE After debugging in Chrome, I am explaining that this is what is passed within javascript, I am not manually setting State to null as it is ing as result from somewhere else as null. Which once again is not in my control as it is ing from database.
While debugging, State displays that it is null, instead of "null", but while debugging in MVC it is displaying "null" instead of null.
$.ajax(
'/Client/Post',
{
method: 'POST',
data: {
Country: 'US',
// this is null because it is ing from somewhere else as null
State: null
}
});
My ASP.NET MVC Handler receives...
public ActionResult Post(Client model){
if(model.State == "null")
{
/// this is true... !!!!
}
if(model.State == null )
{
// :( this should be true...
}
}
Is it problem of ASP.NET MVC or jQuery?
So is it jQuery that sends null as "null" or is it MVC that is setting null as "null"?
SOLUTION
I had to just recursively create new object hierarchy (cloning the object) and send it to jQuery, as jQuery sent data as Form Encoded, in which there is no way to represent null, however ideally jQuery should not have serialized null at all.
I have something wrong with either json or ASP.NET MVC, I am using ASP.NET MVC and here is what I am sending from the client.
NOTE After debugging in Chrome, I am explaining that this is what is passed within javascript, I am not manually setting State to null as it is ing as result from somewhere else as null. Which once again is not in my control as it is ing from database.
While debugging, State displays that it is null, instead of "null", but while debugging in MVC it is displaying "null" instead of null.
$.ajax(
'/Client/Post',
{
method: 'POST',
data: {
Country: 'US',
// this is null because it is ing from somewhere else as null
State: null
}
});
My ASP.NET MVC Handler receives...
public ActionResult Post(Client model){
if(model.State == "null")
{
/// this is true... !!!!
}
if(model.State == null )
{
// :( this should be true...
}
}
Is it problem of ASP.NET MVC or jQuery?
So is it jQuery that sends null as "null" or is it MVC that is setting null as "null"?
SOLUTION
I had to just recursively create new object hierarchy (cloning the object) and send it to jQuery, as jQuery sent data as Form Encoded, in which there is no way to represent null, however ideally jQuery should not have serialized null at all.
Share Improve this question edited Jul 15, 2012 at 3:14 tereško 58.4k25 gold badges100 silver badges150 bronze badges asked Dec 9, 2011 at 15:58 Akash KavaAkash Kava 39.9k20 gold badges124 silver badges171 bronze badges 4- read this one: stackoverflow./questions/801032/null-object-in-javascript – Davide Piras Commented Dec 9, 2011 at 16:02
- http://en.wikipedia/wiki/Http#Technical_overview – Esailija Commented Dec 9, 2011 at 16:02
- if you have to post data than put as json. see the link stackoverflow./questions/8442283/… – Dewasish Mitruka Commented Dec 9, 2011 at 16:10
- I am using both IE9, Chrome, Firefox, all same result. – Akash Kava Commented Dec 9, 2011 at 16:13
3 Answers
Reset to default 6Don't send the field:
$.ajax('/Client/Post',
{
method: 'POST',
data: {
Country: 'US'
}
});
Edit after I re-READ your post
var data = {
Country: 'US',
State: null
}
if (!data.State) delete data.State;
$.ajax('/Client/Post',
{
method: 'POST',
data: data
}
});
This is the same exact principle as above FYI
jQuery does this. You can monitor the request by firebug fiddler, chrome dev helper, etc.
In this case, maybe you could use empty string instead of null
:
data: {
Country: 'US',
State: ""
}
if you have to post data than use object as jSoN see the link
To pass whole JSON objects into controller of MVC
本文标签: javascriptASPNET MVC receives quotnullquot as a string instead of nullStack Overflow
版权声明:本文标题:javascript - ASP.NET MVC receives "null" as a string instead of null - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740207249a2241328.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论