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

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/

10-11 04:05
查看更多