传递数据$ http.post从AngularJS和ASP.net MVC获取null

Lui*_*uis 7 c# asp.net-mvc angularjs angularjs-directive angularjs-controller

我正在尝试将数据从AngularJS传递到ASP.net MVC并且始终为null.这是我的代码(仅发布基本,按钮,控制器和c#:

HTML:

<a class="btn btn-grey btn-lg btn-block" ng-click="AddCar()">Save</a>
Run Code Online (Sandbox Code Playgroud)

调节器

$scope.AddCar = function () {
            $http.post("Cars/AddCar", JSON.stringify($scope.new.JsonCar)).success(function (data) {
                Alert(ok)
            })
Run Code Online (Sandbox Code Playgroud)

C#

public string AddCar(string JsonCar) 
        {
            try
           ....
        }
Run Code Online (Sandbox Code Playgroud)

在JSON.stringify($ scope.new.JsonCar)中,我得到了这个:

"{"名称":"菲亚特500","描述":"新车","MaxUserCapacity":5,"PhotoPath":"无"}"

我做错了什么?

Dav*_*d L 8

将对象直接作为对象传递而不是对其进行字符串化.当它正在传递时,它是一个字符串,而不是可以正确反序列化的对象.

$http.post("Cars/AddCar", $scope.new.JsonCar).success(function (data) {
            Alert(ok)
        })
Run Code Online (Sandbox Code Playgroud)

创建与您的有效负载匹配的Car对象.序列化程序将为您处理JSON对象.

public Car AddCar(Car car) 
    {
        try
       ....
    }
Run Code Online (Sandbox Code Playgroud)

我的假设是,无论如何,您将字符串反序列化为对象.这只是为您节省了额外的一步.