问题描述
限时送ChatGPT账号..我正在尝试将数据从 AngularJS 传递到 ASP MVC,但始终为 null.这是我的代码(仅发布必要的按钮、控制器和 c#:
i'm trying to pass data from AngularJS to ASP MVC and is always getting null. Here's my code (only posting the essential, button, controller and c#:
HTML:
<a class="btn btn-grey btn-lg btn-block" ng-click="AddCar()">Save</a>
控制器
$scope.AddCar = function () {
$http.post("Cars/AddCar", JSON.stringify($scope.new.JsonCar)).success(function (data) {
Alert(ok)
})
c#
public string AddCar(string JsonCar)
{
try
....
}
在 JSON.stringify($scope.new.JsonCar) 我得到这个:
In JSON.stringify($scope.new.JsonCar) i'm getting this:
"{"Name":"FIAT 500","Description":"Newcar","MaxUserCapacity":5,"PhotoPath":"none"}"
"{"Name":"FIAT 500","Description":"New car","MaxUserCapacity":5,"PhotoPath":"none"}"
我做错了什么?
推荐答案
直接将对象作为对象传递,而不是将其字符串化.由于它现在正在传递,因此它是一个字符串,而不是可以正确反序列化的对象.
Pass your object directly as an object rather than stringifying it. As it's being passed right now, it's a string, not an object that can properly be deserialized.
$http.post("Cars/AddCar", $scope.new.JsonCar).success(function (data) {
Alert(ok)
})
创建一个与您的负载匹配的 Car 对象.序列化程序将为您处理您的 JSON 对象.
Create a Car object that matches your payload. The serializer will handle your JSON object for you.
public Car AddCar(Car car)
{
try
....
}
我的假设是,无论如何,您都会将字符串反序列化为对象.这只是为您节省了额外的步骤.
My assumption is that at some point you are deserializing your string into an object regardless. This just saves you that extra step.
这篇关于从 AngularJS 和 ASP MVC 传递数据 $http.post 为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论