9

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 coming as result from somewhere else as null. Which once again is not in my control as it is coming 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 coming 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...
    }
}

enter image description here

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.

tereško
  • 56,151
  • 24
  • 92
  • 147
Akash Kava
  • 37,127
  • 20
  • 114
  • 162

3 Answers3

6

Don'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

Joe
  • 73,764
  • 18
  • 123
  • 142
  • 1
    Please **READ**, It is coming from database as result from somewhere else, I have just written a smaller code to explain the situation, I cant set it as null, also when result is retrieved back from MVC, JSON parses State back to null correctly instead of "null". – Akash Kava Dec 09 '11 at 16:11
  • @AkashKava, learn how http works. ALL the messages between server and client are PURE strings ONLY. I already linked this for you. You need explicit parsing to convert the string `"null"` to ASP.NET `null` value, since it doesn't apparently automatically do it for you. JSON parses PURE string into javascript object presentation, so of course "null" sent from server is real null value in javascript. So much rage... – Esailija Dec 09 '11 at 16:15
  • @Esailija, learning HTTP has nothing to do with this, we have exact same application that works on iOS, Flex, Silverlight as well, everywhere null is transformed correctly, it has nothing to do with my learning of http, it is simple that json has correct provision for null and it should be parsed or transformed correctly. You can see the screen shot, it is either jQuery or mvc who is doing it wrong. – Akash Kava Dec 09 '11 at 16:21
  • @AkashKava, `"country=US&State=null"`, is not json. Json sent to a server would look like `'json={"country": "US", "State": null}'` (pre encoded) Note that it's still a string because that's the only thing that you can send to a server... then you need to do json decode on it on server. – Esailija Dec 09 '11 at 16:29
  • @Mike, well I will have to do this recursively because these objects are built dynamically, I am not manually sending them, they are part of my framework where things work correctly. I will perform this hack anyway but I just want to get to bottom of this as if this is an error in jQuery then my hack will be an unnecessary code, also what if the problem lies in MVC? I want to write hack correctly. – Akash Kava Dec 09 '11 at 16:29
  • Yeah, the only other thing I'm thinking is that you could override the deserialization of your model. But either way it's just a work around. – Joe Dec 09 '11 at 16:31
  • @Esailija, am sorry you are right, jQuery is sending a POST as Encoded Form, instead of sending it as json, sending json will work correctly, unfortunately I have found out more problems with the form post. I thought setting dataType to 'JSON' will send object as json instead of encoded form. Anyway I will accept Mike's answer as that is the only solution, but instead I will have to perform deep clone as I have some extra data that is getting passed. – Akash Kava Dec 09 '11 at 16:47
4

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: ""
}
Jeffrey Zhao
  • 4,651
  • 3
  • 25
  • 43
3

if you have to post data than use object as jSoN see the link

To pass whole JSON objects into controller of MVC

Community
  • 1
  • 1
Dewasish Mitruka
  • 2,416
  • 1
  • 14
  • 20