2017-03-31 88 views
-3

我想从angularjs传递多个参数到asp mvc控制器使用viewmodel,但它不工作。为了参考,请查看html页面的图像。 Html page从angularjs传递多个参数到asp mvc控制器

我想传递OrderId和Id和Rating数组。

下面是HTML代码:

Html Code

这是我angularjs控制器和工厂代码:

Angular Code

下面是两种视图模型类:

viewmodel classes

viewmodel classes

这里是MVC控制器:

enter image description here

正如你看到的,我不能够从视图模型获得订单明细。那么如何得到它?

+0

当你在浏览器中进行调试时,进行AJAX调用时'd'中有什么? – David

+0

你在使用EntityFramework吗?如果是这样,是来自不同表格的“OrderDetails”?我相信你需要使用'include(x => x.OrderDetails)'来提醒它由于Eager Loading ... –

+0

以及我可以看到以下内容:新:{ OrderId:test ProductRating:{ 0:2 1:4 2:3 } } @david –

回答

0

请尝试如下。而推只是识别重复的数据。

var app = angular.module('myApp', []); 
 
app.controller('testCtrl', function($scope) { 
 
$scope.company = [{id:1, name:"Sole Trader", rating:4},{id:2, name:"Pvt Ltd", rating:3},{id:3, name:"LLC", rating:5}]; 
 
$scope.Newrat = []; 
 
    $scope.addRatingbyId = function(pushdata){ 
 
    if($scope.Newrat.length != 0){ 
 
    angular.forEach($scope.Newrat, function(value, index){ 
 
    if(value.id == pushdata.id){ 
 
    $scope.Newrat[index].rating = pushdata.rating; 
 
    }else{ 
 
    $scope.Newrat.push(pushdata); 
 
    } 
 
    }); 
 
}else{ 
 
$scope.Newrat.push(pushdata); 
 
} 
 
    } 
 
    $scope.sendRating= function(){ 
 
    //send $scope.Newrat 
 
    console.log($scope.Newrat); 
 
    } 
 
});
<html> 
 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> 
 
<body> 
 

 
<div ng-app="myApp" ng-controller="testCtrl" > 
 
{{company}} 
 
<table><tr><th>id</th><th>Name</th><th>Rating</th> 
 
</tr> 
 
<tr ng-repeat="com in company"> 
 
<td>{{com.id}}</td> 
 
<td>{{com.name}}</td> 
 
<td> 
 
<input type="text" ng-model="com.rating" ng-change="addRatingbyId(com)"> 
 
</td> 
 
</tr></table> 
 
<input type="submit" ng-click="sendRating()"> 
 
{{Newrat}} 
 
</div> 
 

 
</body> 
 
</html>

+0

是的,现在我能够获得com.id,但仍然无法将多个$范围数据发送到工厂并接收它mvc控制器。我想做这样的事情$ scope.click = function(){ testfac.submit($ scope.Company,$ scope.Newrat).then(function(d){ // do something }) }; fac.submit = function(d){ return $ http('/ Test/Submit', method''POST', data:JSON.stringify({t:$ scope.Company,o:$ scope.Newrat}), }); }; 返回fac –

+0

做它控制器本身。像这样$ scope.click = function(){var data = JSON.stringify({t:$ scope.Company,o:$ scope.Newrat}); testfac.submit(data).then(function(d){// do something})}; fac.submit = function(getData){return $ http({url:'/ Test/Submit',方法:'POST',data:getData)}; –

0

好吧,我发现了它。

$scope.click = function() { // button click function 
     var data = { 
      OrderId: $scope.new.OrderId, 
      OrderDetails: $scope.Company 
     } 
     testfac.submit(data).then(function (d) { //factory calling 
      console.log(d) 
     }) 
    }; 

我们必须在一个变量(var数据)中合并不同的$ scope并传递它。

相关问题