【问题标题】:c# MVC recieving json array in controllor from angularjsc#MVC从angularjs接收控制器中的json数组
【发布时间】:2016-01-07 11:27:07
【问题描述】:

我正在做一个购物网站,我有一个产品模型:

private string _catalog;
    [Key]
    [Required]
    [RegularExpression("^[0-9]{4}$",ErrorMessage="Catalog number must contain 4 digits")]
    public string Catalog
    {
        get
        {
            return _catalog;
        }
        set
        {
            _catalog = value.ToString();
        }
    }
    [Required]
    public string Name { get; set; }
    [Required]
    public double Price { get; set; }
    [Required]
    public int Quantity { get; set; }
    public string Image { get; set; }

我在 AngularJS 中定义了一个 json 数组

$scope.Cart=[{}];

并通过

将商品添加到购物车
$scope.Cart.push(...);

现在我有我的发布命令了:

$http({ method: "POST", data: $scope.Cart, url: "Buy" })
            .success(function (data, status, headers, config) {
                $scope.lock = false;
            });

而动作是:

public ActionResult Buy(List<Product> pr){
     return Content("Buy");
}

但公关列表为空。

我该如何解决?

【问题讨论】:

  • AJAX 请求的确切负载是什么?您可以在Network 标签中看到它。

标签: c# angularjs json asp.net-mvc


【解决方案1】:

修复它。我尝试使用 angular.toJson() 仍然为空,但随后我删除了始终为空的 0 元素,并将其发送到帖子,终于可以工作了。

            $scope.Cart.splice(0, 1);
            var param = angular.toJson($scope.Cart);
            $http({ method: "POST", data: param, url: "Buy" })
            .success(function (data, status, headers, config) {
                $scope.lock = false;
            });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 2015-11-15
    • 2019-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-01
    相关资源
    最近更新 更多