string.empty converted to null when passing JSON object to MVC Controller

前端 未结 5 759
野趣味
野趣味 2020-11-30 01:03

I\'m passing an object from client to server. Properties of the object which are represented as string.empty are being converted to null during this process. I was wondering

5条回答
  •  北海茫月
    2020-11-30 01:59

    When posting data with $.ajax, null is not a possible value for a property of the data option. If you look at the request with an http debugger, you will see that it's converted to an empty string.

    So I guess that your MVC controller is applying the opposite conversion.

    What I do in an ajax application to workaroung this issue is that I don't use the data option of $.ajax(), but I serialize everything in JSON and put it into a single field "data" of the data option. Like that you don't have problems with null values. Of course, you have to deserialize on the server side.

提交回复
热议问题