【问题标题】:Data is always null when send from view to web api controller从视图发送到 Web api 控制器时,数据始终为空
【发布时间】:2016-02-10 23:17:22
【问题描述】:

当我将数据从我的视图发送到 web api 控制器时,我的 ID 字段在控制器中总是为空,下面是我的代码

$scope.Create_Click = function (CategoryselectedItemvalue, SupplierSelectedItemvalue, Product_Name, Quantity_PerUnit, Reorder_Level, Unit_Price, Units_InStock, Units_OnOrder) {
            var CategoryID = parseInt(CategoryselectedItemvalue); 
            var SupplierID = parseInt(SupplierSelectedItemvalue); 
            var ProductName;
            var QuantityPerUnit;
            var ReorderLevel;
            var UnitPrice;
            var UnitsInStock;
            var UnitsOnOrder;
            Product = {
                CategoryID: CategoryID,
                SupplierID: SupplierID,
                ProductName: Product_Name,
                QuantityPerUnit: Quantity_PerUnit,
                ReorderLevel: Reorder_Level,
                UnitPrice: Unit_Price,
                UnitsInStock: Units_InStock,
                UnitsOnOrder: Units_OnOrder
            };
            $http({
                method: 'POST',
                url: '/api/Products/PostProduct',
                data: JSON.stringify($scope.Product),
                headers: { 'Content-Type': 'application/JSON' }
            }).
            success(function (data) {
                alert("Record Added");
            }).
            error(function (msg) {
                alert(msg);
            });
        };

    });

下面是我的控制器方法(这里当我收到数据 CategoryID 并且 SupplierID 始终为空)

[ActionName("PostProduct")]
        public IHttpActionResult PostProduct(Product product)
        {
            Product pro = new Product();
            pro.CategoryID = product.CategoryID;
            pro.SupplierID = product.SupplierID;
            pro.ProductName = product.ProductName;
            pro.QuantityPerUnit = product.QuantityPerUnit;
            pro.ReorderLevel = product.ReorderLevel;
            pro.UnitPrice = product.UnitPrice;
            pro.UnitsInStock = product.UnitsInStock;
            pro.UnitsOnOrder = product.UnitsOnOrder;
            if (repo.AddNewProduct(pro))
            {
                return Ok("Product Added");
            }
            else
            {
                return Ok("Error");
            }
        }

【问题讨论】:

    标签: javascript jquery json angularjs model-view-controller


    【解决方案1】:

    由于您的标头是“应用程序/json”,我认为不需要使用 JSON.stringify,它基本上将 json 转换为字符串,因此您无法访问您的密钥。

    只需以 JSON 格式发送您的对象。

    【讨论】:

      【解决方案2】:

      在对数据进行字符串化时,它应该是 JSON 格式,其中它的键是 product(动作参数名称)

      data: JSON.stringify({product : $scope.Product}),
      

      或者如果您使用Web.API,则不需要对数据进行字符串化,您只需要在Product 参数之前使用[FromBody] 属性即可。

      [ActionName("PostProduct")]
      public IHttpActionResult PostProduct([FromBody] Product product)
      {
         //code is the same
      }
      

      【讨论】:

        猜你喜欢
        • 2016-05-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多