ASP.NET MVC接收和QUOT;零"作为一个字符串,而不是空作为一个、字符串、而不是、NET

由网友(哥有道)分享简介:我有什么毛病或者JSON或ASP.NET MVC中,我使用的ASP.NET MVC和这里就是我从客户端发送。I have something wrong with either json or ASP.NET MVC, I am using ASP.NET MVC and here is what I am send...

我有什么毛病或者JSON或ASP.NET MVC中,我使用的ASP.NET MVC和这里就是我从客户端发送。

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.

注意 在Chrome调试完毕后,我解释说,这就是传递中的JavaScript,我不手动设置状态为空,因为它是从别的什么地方来的结果为空。这再次是不是在我的控制,因为它是从数据库中来了。

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.

在调试状态显示,这是不是空空,但同时在MVC调试它会显示无效,而不是空。

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
             }
   });

我的ASP.NET MVC处理程序接收...

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...
    }
}

它是ASP.NET MVC或jQuery的?

Is it problem of ASP.NET MVC or jQuery?

那么,它的jQuery发送null作为空或者是MVC被设置为null空?

So is it jQuery that sends null as "null" or is it MVC that is setting null as "null"?

解决方案:

我只是递归创建新的对象的层次结构(克隆的对象),并传送给jQuery的,因为jQuery的发送数据表格恩codeD,其中有没有办法重新present空,然而,理想的jQuery不应该序列化空的。

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.

推荐答案

不要发送领域:

$.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

阅读全文

相关推荐

最新文章