2014-07-25 66 views
0

開發web API和angularjs應用,我想張貼SomethingModel的列表,以節省服務器端,這裏是操作方法:的Web API模型綁定不綁定列表

[HttpPost] 
public void SendMessage(List<SomethingModel> model) 
{ 
    //model is null here 
} 

有在請求主體的數組,但如果我添加ModelBinding屬性作爲模型中的不綁定:

public void SendMessage([ModelBinder]List<SomethingModel> model) 

我有一個空的列表,所以這個問題是我應該如何使Web API綁定複雜對象的列表?

這裏是angularjs控制器

myControllers.controller('messageCtrl', ['$scope','somethings', 'Message', 
function ($scope, somethings, Message) { 
    $scope.somethings = somethings; 
    var message = new Message($scope.somethings); 
    $scope.save = function() { 
     message.$save().then(function() { 
      $scope.go('/somewhere); 
     }); 
    } 
} 
]); 

和視圖:

<table> 
    <thead> 
     <tr role="row"> 
      <th></th> 
      <th>Name</th> 
      <th>Title</th> 
     </tr> 
     </thead> 
     <tbody> 
     <tr ng-repeat="sth in somethings"> 
      <td><input type="checkbox" id="opt00" name="selected" ng-model="sth.Selected"/></td> 
      <td class="sorting_1">{{sth.Name}}</td> 
      <td class=" ">{{sth.Title}}</td> 
     </tr> 
    </tbody> 
</table> 

如果我改變按摩服務實例化到

var message = new Message(new Array($scope.somethings)); 

那麼這將是JSON對象,我得到:

{ 
    "0": [ 
    { 
     "Selected": false, 
     "Name": "رضا", 
     "Title": "دوستانی" 
    }, 
    { 
     "Selected": true, 
     "Name": "name2", 
     "Title": "title2" 
    } 
] 
} 

更新:

消息服務:

myServices.factory('Message', ['$resource', 
function ($resource) { 
    return $resource('/api/ManualMessage/SendMessage', null); 
}]); 

回答

0

試試這個:

[HttpPost] 
public void SendMessage([FromBody] List<SomethingModel> model) 
{ 
    //model is null here 
} 
+0

嘗試過,但沒有奏效。 – VahidNaderi