Send a list to a web api using $ http.put of AngularJS

2

I'm having trouble getting a list using the $ http.put method of angularjs, I can send an object normally, but a list is not following, does anyone have any code examples so I can clear this question or pass a link of some material about, from now on I leave my thanks ...

Following the request of Paul follows:

I have a list that I present on the screen in which I will search through web api using AngularJs to make the request get, follows:

$http.get(urlService + "?idUsuario=" + usuario.IdUsuario).success(function (result, status, headers, config) {
            // Ordena pela idade do usuário
            var byId = function (a, b) {
                if (a.IdSolicitacao > b.IdSolicitacao)
                    return -1;
                if (a.IdSolicitacao < b.IdSolicitacao)
                    return 1;
                return 0;
            }
            $scope.listaSolicitacoes = result;//.sort(byId);
            var largura = $('.wraper').width();
            var altura = (43 * largura) / 100;

            $('.panel-body').css('max-height', Math.round(altura) + 'px');
        }).error(function (response) {
            MsgBox("Error", "Ocorreu um problema ao carregar os dados das solicitações.\n" + response, "error");
        });

What happens is that I make direct edits to this list in html and after these changes I get a list (array) with the data of this list already changed ... what happens is that I do not want to send one by one to the I would like to send this list to the web api and there using C # with Entity Framework I would go through this list doing the changes and in the end giving a general SaveChanges (), that is, committing all the changes of once.

If I make a request for each change, the database will be very much requested ...

var UpdateLista = function () {
        $http.put(urlService, angular.toJson($scope.listaSolicitacoes)).success(function (result, status, headers, config) {

        }).error(function (response) {
            MsgBox("Error", "Não foi possível atualizar a prioridade.\n" + response, "error");
        });
    }

WebAPI Role:

public HttpResponseMessage Put(List<SolicitacaoModel> listaSolicitacao)
    {
        try
        {
            using (kurierPortalQualidadeEntities ctx = new kurierPortalQualidadeEntities())
            {
                for (int i = 0; i < listaSolicitacao.Count; i++)
                {
                    Solicitacao sol = new Solicitacao();
                    sol.IdSolicitacao   = listaSolicitacao[i].IdSolicitacao;
                    sol.Assunto = listaSolicitacao[i].Assunto;
                    sol.DataSolicitacao = listaSolicitacao[i].DataSolicitacao;
                    sol.Descricao = ctx.Solicitacao.Where(s => s.IdSolicitacao == sol.IdSolicitacao).FirstOrDefault().Descricao;
                    sol.IdProjeto = ctx.Solicitacao.Where(p => p.DescricaoProjeto == listaSolicitacao[i].Produto).FirstOrDefault().IdProjeto;
                    sol.DescricaoProjeto = listaSolicitacao[i].Produto;
                    sol.IdUsuario = Convert.ToInt32(listaSolicitacao[i].IdUsuario);
                    sol.Usuario = ctx.Usuario.Where(u => u.IdUsuario == sol.IdUsuario).FirstOrDefault();
                    sol.Prioridade = Convert.ToInt32(listaSolicitacao[i].Prioridade);
                    sol.SolicitacaoAprovacao = ctx.SolicitacaoAprovacao.Where(sa => sa.IdSolicitacao == listaSolicitacao[i].IdSolicitacao).ToList();
                    sol.Anexo = ctx.Anexo.Where(a => a.IdSolicitação == sol.IdSolicitacao).ToList();


                    if (!ModelState.IsValid)
                    {
                        return Request.CreateErrorResponse(HttpStatusCode.BadGateway, ModelState);
                    }
                    ctx.Entry(sol).State = EntityState.Modified;
                }
                ctx.SaveChanges();
                return Request.CreateResponse(HttpStatusCode.OK);
            }
        }
        catch (Exception e)
        {
            return Request.CreateErrorResponse(HttpStatusCode.BadGateway, e);
        }
    }

Follow all Exception that returns me:

  

ExceptionMessage: "Multiple actions were found that match the request:   ↵Put on type PortalDeQualidade.Controllers.SolicitacaoController   ↵Put on type PortalDeQualidade.Controllers.SolicitacaoController "   ExceptionType: "System.InvalidOperationException"   Message: "An error has occurred."   StackTrace: "at System.Web.Http.Controllers.ApiControllerActionSelector.ActionSelectorCacheItem.SelectAction (HttpControllerContext controllerContext)   ↵ at System.Web.Http.Controllers.ApiControllerActionSelector.SelectAction (HttpControllerContext controllerContext)   ↵ at System.Web.Http.ApiController.ExecuteAsync (HttpControllerContext controllerContext, CancellationToken cancellationToken)   ↵ at System.Web.Http.Dispatcher.HttpControllerDispatcher.d__1.MoveNext () "

    
asked by anonymous 20.07.2015 / 19:01

0 answers