【问题标题】:Web api model binding doesn't bind the listWeb api模型绑定不绑定列表
【发布时间】:2014-07-25 08:02:42
【问题描述】:

开发 web api 和 angularjs 应用程序,我正在尝试发布SomethingModel 的列表以保存在服务器端,这是操作方法:

[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);
}]);

【问题讨论】:

    标签: angularjs asp.net-web-api model-binding


    【解决方案1】:

    试试这个:

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

    【讨论】:

      猜你喜欢
      • 2018-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-28
      • 1970-01-01
      相关资源
      最近更新 更多