2013-10-31 115 views
0

我无法将数组从angularJS传递到mvc4中的web api动作。将数组传递给web api AngularJS

我的$资源是:

adminApp.factory('presentation', ['$resource', 
function ($resource) { 
    return $resource('/api/presentation/:id', { id: '@id' }, 
     { 
      update: { method: 'PUT', params: { id: '@id' } }, 
      save: { method: 'POST', params: { model1: '@model1' } }, 
      remove: { method: 'DELETE', params: { id: '@id' } } 
     } 

    ); 
}]); 

进行数据的传递数组AngularJS代码来保存它(presentationItemList,我想通过):

$scope.savePresentation = function() { 
     if ($scope.form.$valid) { 

      if ($scope.presentation.Id != undefined && $scope.presentation.Id > 0) { 
       $scope.presentation.$update({ id: $scope.presentation.Id }); 
      } else { 
       $scope.presentation.$save({ model1: presentationItemList }); 
      } 
     } else { 
      $scope.addFormValidationAlert($scope.form); 
     } 
    }; 

我的API动作之后调用save在AngularJS中的操作:

public HttpResponseMessage Post(PresentationItemModel model, List<PresentationElementInfoModel> model1) 
    { 
     var item = Mapper.Map<PresentationItemModel, Presentation>(model); 

     model.Id = GetActionResultData(ServiceDataProvider.PresentationCrudService.SaveOrUpdate(item)); 
     if (model.Id.MoreThanZero()) 
     { 
      return new HttpResponseMessage(HttpStatusCode.Accepted) 
      { 
       Content = new ObjectContent<PresentationItemModel>(model, 
                   new JsonMediaTypeFormatter()) 
      }; 
     } 
     throw new HttpResponseException(HttpStatusCode.NotFound); 
    } 

我的萤火得到一个错误:“不能绑定多个参数(‘模型’和‘MODEL1’)请求的内容”

此代码做工精细,如果我dont't通阵列presentationItemList从角度和不映射它在模型1参数的web API动作。

请帮帮我!

+0

您的资源如何注入?你能告诉我们你的整个控制器吗? (1)你的'presentationItemList'没有在'$ scope'中定义? (2)如果您正在使用您的资源,为什么要调用'$ scope.presentation。$ save'? '$ save',如果你正在使用你的资源,应该只是'save'; – Beterraba

回答

1

$save方法params参数用于绑定到url模板或作为查询字符串传递。做{ model1: presentationItemList }可能无法正常工作,因为我认为该行为将序列化presentationItemList并将其添加到URL。

在角端,您应该添加presentationItemList作为在$ scope.presenation子财产

$scope.presenation.items=presentationItemList

,并做了$save没有参数传递。

WebAPI的问题在于它不能为POST接受多个参数。所以这个列表应该是PresentationItemModel的一部分,而且事情会起作用。

如果你不能改变服务器模型,那么该方法应该是先保存父项,获取父项的id,然后再次调用保存子项列表。

+0

非常感谢! :) – IFrizy

+0

你在POST操作中对两个参数是真实的! – IFrizy