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

标签 asp.net-mvc angularjs

我正在尝试将一些数据从 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/

相关文章:

javascript - 之间的区别!和 !在 JavaScript 中

javascript - 如何在 formData 中追加数组,其中包含 2 个带有键的值

asp.net-mvc - ASP.NET MVC 编辑时保留 mdoel 的 ID 值

c# - ASP.NET MVC DropDownList 在事件时不显示颜色

c# - 像 Jabbr 这样的内容提供商

angularjs - 为什么双向绑定(bind)有时可以在不使用 Angular 中的点的情况下工作?

javascript - 在AngularJS中获取部分url

asp.net-mvc - MVC DataAnnotation 不接受空格

带有 jQ​​uery SimpleModal 和 Ajax.BeginForm Helper 的 ASP.NET MVC

javascript - 如何让 Youtube 播放器弹出并播放视频?