【问题标题】:JSON object not correctly sent to my ASP.NET MVC controller, missing propertiesJSON 对象未正确发送到我的 ASP.NET MVC 控制器,缺少属性
【发布时间】:2016-05-14 07:14:00
【问题描述】:

我在将 JSON 对象传递给我的 ASP.NET MVC 控制器时遇到问题。

JS中JSON是这样设置的:

 jsonChildren = '{ "childImproItems" : [{ "Theme":"tralalali"  }, { "Theme":"tralalali"  }]}';
...
  $.ajax({
           url: url,  
            type: 'POST',
            data: JSON.parse(jsonChildren),
            success: function (result) {
              ...  
            },
            error: function (result) {
              ..
            }
        });

在我的控制器内部:

public int MyMethod(String typeName, SelectOptionsViewModel id) {
  return 0;
}

关注的Model类如下:

public class SelectOptionsViewModel {
        ...
  public List<ChildImproItemViewModel> childImproItems { get; set; }
}

和:

public class ChildImproItemViewModel {
        ...
  public string Theme { get; set; }
}

调试器正确地将我路由到我的控制器并将 JSON 正确解释为 SelectOptionsViewModel 实例。 它使用 2 个对象的列表正确设置其 childImproItems 属性。 但是 2 个 childImproItems 对象的 Theme 属性为 null,尽管在我的示例中它们应该设置为一个虚拟值。 其他属性也是如此。

你们知道我的错误在哪里吗?

提前谢谢。

编辑: 如果我自己创建 JSON 对象,情况完全相同:

   JSONFormatChildren = {};
    JSONFormatChildren.childImproItems = {};
    JSONFormatChildren.childImproItems[0] = {};
    JSONFormatChildren.childImproItems[0].Theme = 'trouloulou';
    JSONFormatChildren.childImproItems[1] = {};
    JSONFormatChildren.childImproItems[1].Theme = 'trouloulou';

然后是ajax:

 $.ajax({
            url: url,  //We can't pass the selectId directly as C# does not know about JS variables
            type: 'POST',
            data: JSONFormatChildren,
....
}

再一次,使用正确数量的元素 (2) 正确创建了列表,但所有元素 Theme property = null 而不是 trouloulou...

编辑 2: 如果我使用这个 JSON:

 jsonChildren = '{"Theme":"tralalali"}';

还有这个方法:

 public int test2(String typeName, ChildImproItemViewModel id) 
    {
        return 0;
    }

效果很好

如果我使用这个 JSON:

 jsonChildren = '[{"Theme":"tralalali"},{"Theme":"tralalali"}]'; 

还有这个方法:

   public int test2(String typeName, List<ChildImproItemViewModel> id) 
    {
        return 0;
    }

它不起作用:调用方法时 id = null

【问题讨论】:

  • json2csharp.com 在使用 json 处理模型绑定器问题时发现有用。这会让你看到你的 json 在 C# 中解析成什么(很明显),这样你就可以看到问题是出在你的实际 json 还是其他东西上。
  • 不错的 URL thx 分享:)。不过,在其中解析我的 JSON 字符串最终会得到明显正确的模型类。
  • @AD 编辑您的问题以将签名包含到您接受 JSON 的 ActionResult 中
  • @AD 我刚刚注意到的另一个问题是您的jsonChildren 试图将 JSON 构造为字符串。忘记那个。只需创建一个 Javascript 对象并将其传递给 jquery ajax 函数。我会更新我的答案
  • @AD 将 contentType: 'application/json' 添加到您的 ajax 中

标签: javascript asp.net json asp.net-mvc asp.net-mvc-3


【解决方案1】:

您需要正确指定json的内容类型,因为jquery中默认是form-url-encoded。另外,我认为 JSON.Parse 函数是不必要的,有/没有它都可以试试。

编辑:我刚刚注意到您正在尝试手动创建 JSON 字符串。这是一个坏主意,只需创建一个 Javascript 对象,然后使用 JSON.stringify()。否则很容易搞砸 JSON 字符串语法。如果您支持旧浏览器,则可能需要包含 json2.js 库

var     jsonChildren = { "childImproItems" 
: [{ "Theme":"tralalali"  }, { "Theme":"tralalali"  }]};
    ...

  $.ajax({
           url: url,  
            type: 'POST',
            data: JSON.stringify(jsonChildren),
            contentType: 'application/json',
            success: function (result) {
              ...  
            },
            error: function (result) {
              ..
            }
        });

【讨论】:

  • 似乎不是这个原因,如果我添加了contentType,那么它会调用错误函数而不是成功函数。
  • @A D 你在有没有 JSON.parse 的情况下都试过了吗?
  • @AD 如果它正在调用 JS 错误函数,那么您的 ActionResult 返回错误代码,可能表明绑定失败,顺便说一句,这是您最初的问题,因为模型绑定不起作用。它可能会导致您遇到真正的问题,例如类型不匹配或其他问题。错误函数中的消息说明了什么?有什么例外?
  • 没有 JSON.parse,更糟糕的是,当 (String typeName, SelectOptionsViewModel id) 被调用时,ido不再将 childImproItems 识别为 List 。所以它必须是 JSON.parse 。
  • 在指定 contentType: 'application/json' 时,如前所述,它会触发错误 gfunction,Chrome 工具仅指定发生了内部错误 500,我没有更多可见的详细信息。如果我不指定它,我认为这不是问题的原因,JSON 被解释为 SelectOptionsViewModel 并且它的 List 是使用正确数量的元素正确创建的。只有每个元素的 Theme 属性不起作用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多