gpt4 book ai didi

asp.net-mvc - 将对象从 AngularJS 传递到 ASP.NET MVC Controller

转载 作者:行者123 更新时间:2023-12-01 12:32:42 25 4
gpt4 key购买 nike

我正在尝试将一些数据从 AngularJS 传递到我的 MVC Controller ,但 MVC Controller 上的 Customer 对象始终为 null。我错过了什么?

Angular

        $scope.new = {};

$scope.AddCustomer = function () {
$http.post('/Customer/SaveCustomer', $scope.new).success(function () {

});
}

HTML

  <input class="form-control" type="text" placeholder="CustomerID" ng-model="new.c.CustomerID" />
<input class="form-control" type="text" placeholder="CompanyName" ng-model="new.c.CompanyName" />
<button type="button" class="btn btn-primary" ng-click="AddCustomer()">Save</button>

C#

 [HttpPost]
public void SaveCustomer(Customer customer)
{
....
}

public class Customer
{
public string CustomerID { get; set; }

public string CompanyName { get; set; }
}

最佳答案

像这样更新您的 HTML :

new.c.CustomerID 更改为 new.CustomerID

<input class="form-control" type="text" placeholder="CustomerID" ng-model="new.CustomerID" />
<input class="form-control" type="text" placeholder="CompanyName" ng-model="new.CompanyName" />

现在这会起作用

$http.post('/Customer/SaveCustomer', $scope.new).success(function () {

});

关于asp.net-mvc - 将对象从 AngularJS 传递到 ASP.NET MVC Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32119662/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com