2
I’m having trouble sending a list using the $http.put method from Angularjs, I can send an object normally, plus a list is not following, someone has some example code so that I can take this doubt or pass a link of some material about, I already leave my thanks...
Meeting 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 edits directly in 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 database to be updated only the data changed, I would like to send this list to the web api and there using C# with Entity Framework I would go through this list making the changes and in the end giving a general Savechanges(), ie committing all the changes at once.
If I make a request for each change will get a lot of request to database...
follows the angular function that makes the request and then the web api in c# as I am implementing:
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");
});
}
Function of the Webapi:
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 the Exception that returns to me:
Exceptionmessage: "Multiple actions Were found that match the request: Put on type Portaldequality.Controllers.Solicitacaocontroller Put on type Portaldequality.Controllers.Solicitacaocontroller" Exceptiontype: "System.Invalidoperationexception" Message: "An error has occurred." Stacktrace: " in System.Web.Http.Controllers.Apicontrolleractionselector.ActionSelectorCacheItem.Selectaction(Httpcontrollercontext controllerContext) System.Web.Http.Controllers.Apicontrolleractionselector.Selectaction(Httpcontrollercontext controllerContext) on System.Web.Http.ApiController.Executeasync(Httpcontrollercontext controllerContext, Cancellationtoken cancellationToken) in System.Web.Http.Dispatcher.Httpcontrollerdispatcher.d__1.Movenext()"
Could you please post what you have so far?:
– PauloHDSousa
Paul, I edited the question, see if you can better understand what I want to do, thank you!
– Roberto Ramos
One way is: you create a new list, and whenever an item is changed, you place it within this list. and at the end you send it to be updated.
– PauloHDSousa
My problem is not getting this list updated but how to send this list to the web api, I’m trying with $http.put as you can see in the code in Updatelista() but the same does not accept sending, always give error in the type I’m sending to web api, I want to know how to send a list using $http.put() from Angularjs...?
– Roberto Ramos
Aaah ta, what mistake does?
– PauloHDSousa
The material in stock is available on the website of Angularjs: https://docs.angularjs.org/api/ng/service/$http https://docs.angularjs.org/api/ngResource/service/$Resource
– Ivan Ferrer
As @Paulohdsousa said in the comments above, it’s a routing problem. The routing algorithm of Asp.net is finding two functions for the PUT method. That being said, it is best to edit the original post and add the routing table (defined in Webapiconfig.Cs or similar) and the skeleton (definition of functions) of the corresponding controller.
– rll