2015-08-20 53 views
2

我试图从AngularJS传递一些数据到我的MVC控制器,但客户对象在MVC控制器上始终为空。我错过了什么?从AngularJS传递对象到ASP.NET MVC控制器

 $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; } 
} 
+1

尝试new.CustomerID和new.CompanyName ... – ssilas777

+0

@ ssilas777感谢...这工作...我不知道为什么这个教程中,我经历了它标记为new.c.customerid – Anonymous

+0

啊,似乎@ ssilas777击败了我! – skubski

回答

3

更新您的HTML这样的:

变化new.c.CustomerIDnew.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() { 

     }); 
0

你的模型似乎是

new = { 
    c : { 
    CustomerID : '', 
    CompanyName : '' 
    } 
} 

哪些不会映射到您的客户类。你应该参考new.CustomerIDnew.CompanyName。此外,我会避免使用新的关键字作为变量名称。

1

首先考虑到骆驼情况下,JavaScript对象

$scope.new = { 
 
    customerID: '', 
 
    companyName: '' 
 
}; 
 

 
$scope.AddCustomer = function() { 
 
    $http.post('/Customer/SaveCustomer', $scope.new).success(function() {});
<!--check the changes in ng-model--> 
 
<input class="form-control" type="text" placeholder="CustomerID" ng-model="new.customerID" /> 
 
<input class="form-control" type="text" placeholder="CompanyName" ng-model="new.companyName" /> 
 
<button type="button" class="btn btn-primary" ng-click="AddCustomer()">Save</button> 
 

 

 

 
<!--Best of Luck-->

相关问题