【问题标题】:Object Array As Parameter to Controller对象数组作为控制器的参数
【发布时间】:2013-09-12 14:42:45
【问题描述】:

如何将对象数组作为参数发送到 MVC 控制器?

public class FeedStats
{
    public long FeedId { get; set; }
    public ApiType ApiType { get; set; }
    public long UserId { get; set; }
    public float ReadTime { get; set; }
    public long FeedIndex { get; set; }
    public bool IsWebRead { get; set; }
}

在控制器中

[HttpPost]
public HttpResponseMessage UpdateFeedStats(FeedStats[] data)
{
}

当我使用这些参数向Postman 发出HttpPost 请求时,数据始终为空。有什么问题?

标题:

Content-Type: application/json

{
data: [
   FeedId: 1,
   ApiType: 1,
   UserId: 1,
   ReadTime: 0.65,
   FeedIndex: 1,
   IsWebRead: 1
]
}

【问题讨论】:

  • 您能说明一下您是如何提出请求的吗?
  • 我用请求标头更新了我的问题。请检查。
  • @haim770,谢谢它的工作,嗨,你可以把它作为答案发布。我会接受的。

标签: c# asp.net-mvc-4


【解决方案1】:

因为FeedStats[] 是一个对象 数组。您还需要用花括号将内部对象括起来:

{
data:[
     {FeedId:1,...},
     {...},
     {...}
     ]
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-12
    • 2013-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多