Search code examples
c#angularjsasp.net-web-apiangular-resource

Custom method in AngularJs with post data


My problem is this. I have in my application a Service using $resource and a controller in web api.

this is my service in angular

GarantiaService = function ($resource) 
{
     return $resource("../../api/garantia/:id", null,
     {
        'update': { method: 'PUT' },
        'renovar': { method: 'RENOVAR'}
     });
};

this is my controller

RenovarController = function ($scope, $routeParams, GarantiaService) {
    $scope.Garantia = { Id: 0 };

    $scope.ActualizarGarantia = function () {

        GarantiaService.renovar({ id: $scope.Garantia.Id }, $scope.Garantia, function () {
            //$location.path('#/');
        }, function () {
            alert("Error en la persistencia");
        });
    };
};

and in web api i created a custom method

    [AcceptVerbs("RENOVAR")]
    public IHttpActionResult Renovar(int id, Garantia garantia)
    {


        return StatusCode(HttpStatusCode.NoContent);
    }

the thing is i keep getting garantia null in c# and i'm not passing a null object in angular. Is it a problem in my service or in c#?

Thank you in advanced.


Solution

  • I solved it like this

    jscontroller

    RenovarController = function ($scope, $routeParams, GarantiaService) {
        $scope.Garantia = { Id: 0 };
    
        $scope.ActualizarGarantia = function () {
    
            GarantiaService.renovar({ garantia: $scope.Garantia }, function () {
                //$location.path('#/');
            }, function () {
                alert("Error en la persistencia");
            });
        };
    };
    

    web api:

    [AcceptVerbs("RENOVAR")]
    public IHttpActionResult Renovar(string garantia)
    {
         Garantia deser = (Garantia)JsonConvert.DeserializeObject(garantia,typeof(Garantia));
    
         return StatusCode(HttpStatusCode.NoContent);
    }