javascript - JSON 中的数组接收为空
问题描述
当我从浏览器 (JavaScript) 向 ASP.NET MVC 后端提交包含一些原始类型和一个对象数组的表单时,我正在发送一个 JSON 对象,但是对象数组被反序列化为一个空数组(实际上是列表)。
我已经使用 Chrome 的检查器检查了发送的内容,并且 JSON 以正确的值输出,所以我相信问题出在后端。
控制器动作:
[HttpPost]
public ActionResult FooAction(FooViewModel viewModel)
{
//code
}
FooView 模型:
public class FooViewModel
{
public List<BarViewModel> Bars { get; set; } // <-- What arrives empty, not null
public int PrimitiveProperty { get; set; }
public int OtherPrimitiveProperty { get; set; }
public string LastPrimitiveProperty { get; set; }
}
条形视图模型:
public class BarViewModel
{
public long LongProperty{ get; set; }
public string StringProperty { get; set; }
public bool BoolProperty { get; set; }
}
调试控制器的动作时,Bars
为空(非空)。
这就是 Chrome 的检查员向我展示的内容:
LastPrimitiveProperty: SomeText
PrimitiveProperty: 1
OtherPrimitiveProperty: 9
Bars: [{"LongProperty":274,"StringProperty":"SomeString","BoolProperty":true},{"LongProperty":119,"StringProperty":"SomeString","BoolProperty":false},{"LongProperty":163,"StringProperty":"SomeString","BoolProperty":false}]
解决方案
[HttpPost]
public ActionResult FooAction([FromBody]FooViewModel viewModel)
{
//code
}
试试这个,它应该收集 json 对象
推荐阅读
- javascript - 相对于另一个不是其父元素的元素定位一个元素
- r - “使用 rnoaa/countyweather 包在 R 中获取纽约市的每小时数据”
- python-3.x - 如何解决python中没有名为“replacers”的模块问题?
- php - 使 CPT 存档可编辑并集成 Thrive Architect
- ubuntu - 如何在 Win 10 中使用 WSL Ubuntu Bash 开发 Go App
- javascript - 如何从文本框中提取选择性数据
- android - 通过刷新(或不刷新)片段来实现动态 ScrollView
- javascript - 如何在 java 脚本中的自定义 HTMLElement 中为锚点附加点击处理程序?
- php - 为什么这两个脚本中只有一个有效?
- javascript - Chrome 扩展 - 文本输入和悬停事件在外部显示器上没有响应