Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc $http post未与asp.net MVC模型绑定_Asp.net Mvc_Angularjs_Asp.net Mvc 4 - Fatal编程技术网

Asp.net mvc $http post未与asp.net MVC模型绑定

Asp.net mvc $http post未与asp.net MVC模型绑定,asp.net-mvc,angularjs,asp.net-mvc-4,Asp.net Mvc,Angularjs,Asp.net Mvc 4,为什么angularjs$http post的有效负载没有绑定到输入模型 调用该操作时,模型为null,并且request.params和request.forms不显示任何发送表单的迹象。但是fiddler请求显示负载是用JSON发送的 AngularJS: $http({ method: "POST", url: "price/add", data: { Id: $scope.id,

为什么angularjs$http post的有效负载没有绑定到输入模型

调用该操作时,模型为null,并且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; }
    }
控制器和动作方法说明

public class PriceController : Controller
    {
        [HttpPost]
        public int Add(PriceModel price)
        {
小提琴手:

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}

我不确定模型绑定是否混淆,因为参数名为
price
,并且在
PriceModel
中有一个属性,也称为
price
。您可以尝试重命名操作参数名称吗?

为了节省另一个人一个小时的时间,导致此问题的一个常见原因是我们使用

http({
url:url,
方法:“POST”,
参数:参数,
标题:{
“X-request-With':“XMLHttpRequest”
}
}).成功(功能(数据){
延迟。解析(数据);
}).错误(功能(数据、状态){
延迟。拒绝(数据);

});
我不确定模型绑定是否混淆,因为参数名为
price
,并且您在
PriceModel
中有一个属性,也称为
price
。可以尝试重命名动作参数名称吗?没错!发布答案并获得奖励:)重命名参数名称很有魅力!我把名字从price改为model,结果成功了。不知道!其他一切似乎都是正确的,这是唯一可疑的事情。很高兴它有帮助!