将对象从AngularJS传递到ASP.NET MVC控制器 [英] Passing object from AngularJS to ASP.NET MVC Controller

查看:52
本文介绍了将对象从AngularJS传递到ASP.NET MVC控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一些数据从AngularJS传递到我的MVC控制器,但是Customer对象在MVC控制器上始终为null.我想念什么?

I'm trying to pass some data to my MVC controller from AngularJS but the Customer object is always null on the MVC controller. What am I missing?

角度

        $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 () {

        });

这篇关于将对象从AngularJS传递到ASP.NET MVC控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆