【问题标题】:sending array to mvc .net using $http post returning null使用 $http post 将数组发送到 mvc .net 返回 null
【发布时间】:2014-12-16 23:24:56
【问题描述】:

我有一个 MVC 动作,它需要一个 int 数组(int[] ids)。我正在使用 angularjs 的 $http 帖子,但每次我发布它都会返回一个 null

$http({
            method: 'POST',
            url: url,
            headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8;' },
            data: $.param({
                pcode: $scope.pcode,
                bId: $scope.bId,
                brandIds: [898,55],
                regId: $scope.regId,
                __RequestVerificationToken: getVerificationToken() // this is Html.Antiforgery that the action needs for posts
            })
        }).success(function (result) {});

我试过这个解决方案

AngularJs $http.post() does not send data

但是如果我注入 $httpProvider 会引发注入错误。我正在使用 angularjs 1.3

【问题讨论】:

    标签: asp.net-mvc angularjs angularjs-service


    【解决方案1】:
    $http({
                method: 'POST',
                url: url,
                headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8;' },
                data: $.param({
                    pcode: $scope.pcode,
                    bId: $scope.bId,
                    brandIds: [898,55],
    
                    regId: $scope.regId,
                    __RequestVerificationToken: getVerificationToken() // this is Html.Antiforgery that the action needs for posts
                },true)
            }).success(function (result) {});
    

    当您不提供该参数时,它将对数组brandids[] 进行编码。您可以在 request.form 中看到这一点。这不适用于 mvc 模型绑定。

    如果您提供该值,则它将数组编码为品牌标识。这是 mvc 模型绑定器所需要的,因此它可以工作。

    【讨论】:

    • 帖子上仍然为空。 formCollection 将其显示为数组,但如果在 int[] brandIds 参数上,则为 null。
    • 检查更新。我已将 true 作为 $.param 的第二个参数传递
    • 我会尝试一下。那个参数有什么作用?我似乎找不到有关它的文档?
    猜你喜欢
    • 2013-06-14
    • 2011-04-22
    • 1970-01-01
    • 1970-01-01
    • 2015-11-27
    • 2016-05-13
    • 2016-08-04
    • 2023-04-09
    相关资源
    最近更新 更多