为什么来自 angularjs $http 帖子的有效负载没有绑定(bind)到输入模型?
当调用该操作时,模型为空,并且 request.params 和 request.forms 没有显示任何已发送表单的迹象。但是 fiddler 请求显示负载正在使用 JSON 发送。
AngularJS:
$http({
method: "POST",
url: "price/add",
data: {
Id: $scope.id,
StoreId: $scope.storeid,
Name: $scope.name,
Manufacturer: $scope.manufacturer,
Price: $scope.price
}
})
模型:
public class PriceModel
{
public int? Id { get; set; }
public int? StoreId { get; set; }
public string Barcode { get; set; }
public string Name { get; set; }
public string Manufacturer { get; set; }
public DateTime Created { get; set; }
public double? Price { get; set; }
}
Controller 和 Action 方法描述
public class PriceController : Controller
{
[HttpPost]
public int Add(PriceModel price)
{
fiddler :
POST http://localhost:4989/price/add HTTP/1.1
Host: localhost:4989
Connection: keep-alive
Content-Length: 70
Accept: application/json, text/plain, */*
Origin: http://localhost:4989
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.146 Safari/537.36
Content-Type: application/json;charset=UTF-8
Referer: http://localhost:4989/
Accept-Encoding: gzip,deflate,sdch
Accept-Language: nb,no;q=0.8,en-US;q=0.6,en;q=0.4
{"id":"","storeid":"","name":"asdf","manufacturer":"asdf","price":123}
最佳答案
我不确定模型绑定(bind)是否混淆,因为参数名为 price
并且您在 PriceModel
中有一个属性这也称为 Price
.您可以尝试重命名操作参数名称吗?
关于asp.net-mvc - $http post 未与 asp.net MVC 模型绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22390777/