Javascript 从视图发送到web api控制器时,数据始终为空
当我从视图向web api控制器发送数据时,控制器中的ID字段总是为空,下面是我的代码Javascript 从视图发送到web api控制器时,数据始终为空,javascript,jquery,json,angularjs,model-view-controller,Javascript,Jquery,Json,Angularjs,Model View Controller,当我从视图向web api控制器发送数据时,控制器中的ID字段总是为空,下面是我的代码 $scope.Create_Click = function (CategoryselectedItemvalue, SupplierSelectedItemvalue, Product_Name, Quantity_PerUnit, Reorder_Level, Unit_Price, Units_InStock, Units_OnOrder) { var CategoryID =
$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始终为空时)
当字符串化数据时,它应该是JSON格式,其中它的键应该是
product
(操作参数名称)
或者,如果使用的是Web.API
,则不需要对数据进行字符串化,只需在产品
参数生效之前使用[FromBody]
属性即可
[ActionName("PostProduct")]
public IHttpActionResult PostProduct([FromBody] Product product)
{
//code is the same
}
当字符串化数据时,它应该是JSON格式,其中它的键应该是
product
(操作参数名称)
或者,如果使用的是Web.API
,则不需要对数据进行字符串化,只需在产品
参数生效之前使用[FromBody]
属性即可
[ActionName("PostProduct")]
public IHttpActionResult PostProduct([FromBody] Product product)
{
//code is the same
}
由于您的标题是“application/json”,我认为不需要使用
json.stringify
,它基本上将json转换为字符串,因此您无法访问密钥
只需按JSON格式发送对象。由于您的标题是“application/JSON”,我认为不需要使用
JSON。stringify
基本上将JSON转换为字符串,因此您无法访问密钥
只需按JSON格式发送对象即可
[ActionName("PostProduct")]
public IHttpActionResult PostProduct([FromBody] Product product)
{
//code is the same
}