【问题标题】:POST a List<T> to MVC Controller always null将 List<T> 发布到 MVC 控制器始终为空
【发布时间】:2013-01-12 20:58:15
【问题描述】:

我正在尝试将列表发布到我的 MVC 控制器..

控制者: // POST api/UserFollows

public HttpResponseMessage PostUserFollows(List<FollowItem> followItemList)
{
   //I'M GETTING NULL IN followItemList
   if(followItemList==null)
   {
      return Request.CreateResponse(HttpStatusCode.BadRequest);
   }
}

输出:

STATUS 400 Bad Request <-- Means I got null in followItemList
TIME 4025 ms
Cache-Control →no-cache
Connection →Close
Content-Length →0
Date →Tue, 29 Jan 2013 09:38:31 GMT
Expires →-1
Pragma →no-cache
Server →ASP.NET Development Server/10.0.0.0
X-AspNet-Version →4.0.30319

FollowItem

namespace Helpers.SubClasses
{
    public class FollowItem
    {
        public bool action;
        public long FollowsUserId;
    }
}

我尝试了很多请求,但没有一个有效..我总是得到空!

发布方法:

function postFollowList() {
            $.ajax( {
            url: Request_Url + "api/UserFollows",
            type: 'post',
            data: {
                    {action: true, FollowsUserId: 123456777},
                    {action: true, FollowsUserId: 123456888}
            },
            dataType: 'json',
            success: function( data )
            {
                $('#newmovie').html("OK");
            },
            error: function (jqXHR, textStatus, err) 
            {
                $('#newmovie').html('Error: ' + err);
            }
             });

请求: //作为 JSON - 我正在使用 POSTMAN

1.
    [
        {"action":"true","FollowsUserId":"123456777"}
    ]
2.
    [
        {action: true, FollowsUserId: 123456777},
        {action: true, FollowsUserId: 123456888}
    ]
3.
    {[
        {action: true, FollowsUserId: 123456777},
        {action: true, FollowsUserId: 123456888}
    ]}
4.
    {followItemList:[
        {action: true, FollowsUserId: 123456777},
        {action: true, FollowsUserId: 123456888}
    ]}

null 示例:

我尝试了很多.. 有人可以帮我吗? 谢谢!!!

编辑: 答案是当我需要发送 application/json 时,我在 content-type 中发送了 application/xml

【问题讨论】:

  • 什么时候得到空值?您的代码在做什么?“我得到空值”是什么意思?
  • 你是怎么发帖的?请显示完整代码。
  • @RoyDictus 添加了更多信息
  • @ken2k 添加了 Post 方法 谢谢!

标签: c# asp.net-mvc json entity-framework jquery


【解决方案1】:

JSON 似乎无效。也许试试这个:

[
    {
        "action": true,
        "FollowsUserId": 123456777
    },
    {
        "action": true,
        "FollowsUserId": 123456888
    }
]

检查 JSON 有效性的好工具是 jsonlint.com

【讨论】:

  • 我仍然得到一个空列表。我在问题中添加了一个图像。谢谢!
  • 对于 POST,您应该发送值为“application/json”的“Content-Type”。
  • 我的错.. 列表与 FollowItem 的类型不同。并且“Content-Type”的值为“application/xml”感谢您的帮助并感谢 JSON 的有效性
【解决方案2】:

我试过了 -

public class HomeController : Controller
{
    //
    // GET: /Home/

    public ActionResult Index()
    {
        var foo = new List<FollowItem>()
        {
            new FollowItem {action = true, FollowsUserId = 123456777},
            new FollowItem {action = true, FollowsUserId = 123456888}
        };
        return new JsonResult {Data = foo, JsonRequestBehavior = JsonRequestBehavior.AllowGet};
    }

    //
    // POST: /Home/
    public ActionResult Dump(List<FollowItem> followItems)
    {
        Debug.WriteLine(followItems);
        return new HttpStatusCodeResult(200);
    }

}
public class FollowItem
{
    public bool action;
    public long FollowsUserId;
}

然后发布这个 -

[
  {"action":true,"FollowsUserId":123456777},
  {"action":true,"FollowsUserId":123456888}
]

这很有效。请注意,这也是它发送响应的方式。

【讨论】:

  • 嗯...我正在尝试 POST 到我的控制器看来您正在展示如何根据请求发布 JSON 字符串
  • Dump 动作需要一个 POST,Index 动作只是给你序列化的 JSON,你可以回发。
  • 我尝试发布到它.. 我总是进入public ActionResult Index()
  • 你应该发布到 ~/Home/Dump。默认操作是索引
  • 谢谢.. 我的问题是内容类型。但感谢您提供新信息!
【解决方案3】:

尝试将变量名称添加到数据中。请参阅以下代码。

function postFollowList() {
        $.ajax( {
        url: Request_Url + "api/UserFollows",
        type: 'post',
        data: { followItemList: [
            {action: true, FollowsUserId: 123456777},
            {action: true, FollowsUserId: 123456888}
        ]},
        dataType: 'json',
        success: function( data )
        {
            $('#newmovie').html("OK");
        },
        error: function (jqXHR, textStatus, err) 
        {
            $('#newmovie').html('Error: ' + err);
        }
});

编辑:也许你可以在发布之前尝试序列化数组:

        data: { followItemList: JSON.stringify([
            {action: true, FollowsUserId: 123456777},
            {action: true, FollowsUserId: 123456888}
        ])},

【讨论】:

  • 嗨!谢谢你的帮助,现在我进入控制器followItemList.Count = 0你认为它是什么?
  • Still Count=0 也许调用不识别操作,FollowsUserId vars?
  • 谢谢!问题出在 content-type 中。谢谢!
猜你喜欢
  • 2019-11-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多