无法从angularJS中的$ http将数据传递到as.net mvc控制器

时间:2017-03-24 03:55:51

标签: angularjs asp.net-mvc

当我将数据从angular js传递给action方法时,我在action方法中获取null值。数据来到$ scope.custmodel,我调试并看到它命中AddCutomer方法,但数据为空。任何人都可以帮我解决这个问题

Admin.js代码

var app = angular.module("adminmdl", [])
app.controller("admincontroller", function ($scope,AdminService) {
    $scope.Action = 'Add';
    $scope.data = {
        cus_code: '',
        cus_name: '' 
    }
    $scope.savecu = function () {
        AdminService.saveCustomerDdetails($scope.cusmodel).then(function (data) {

            if (data != null) {
                alert('Insert successfully');
            } else {
                alert('error in inerting data');
            }
        });
    }
})
.factory("AdminService", function ($http) {

    var fact = {};
    fact.saveCustomerDdetails = function (d) {
        return $http({
            url: '/Admin/AddCutomer',
            method: 'POST',
            data: JSON.stringify(d),
            headers: { 'content-type': 'application/json' }
        });
    };
    return fact;
});

ASP MVC方法

   [HttpPost]
        public JsonResult AddCutomer(Customer customer) {
                te.Customers.Add(customer);
                te.SaveChanges();
                string message = "Success";
            return new JsonResult { Data = message, JsonRequestBehavior = JsonRequestBehavior.AllowGet };


        }

html代码

<form class="form-horizontal" method="post" action="#" name="basic_validate" id="basic_validate" novalidate="novalidate">

                                 <div class="control-group">
                                    <label class="control-label">Customer Code</label>
                                    <div class="controls">
                                        <input type="text" ng-model="cusmodel.Customercode" name="required" id="required" >
                                    </div>
                                </div>
                                <div class="control-group">
                                    <label class="control-label">Customer Name</label>
                                    <div class="controls">
                                        <input type="text" ng-model="cusmodel.Customername" name="name" id="name" >
                                    </div>
                                </div>
                                <div class="control-group">
                                    <div class="controls">
                                        <input type="submit" value="Save" ng-click="savecu()" class="btn btn-success">
                                        <input type="submit" value="Clear" class="btn btn-success" />
                                    </div>
                                </div>

1 个答案:

答案 0 :(得分:0)

将[FromBody]属性添加到控制器,如下所示

public JsonResult AddCutomer([FromBody]客户客户)