AngularJs 中使用 post 数据的自定义方法

Custom method in AngularJs with post data

我的问题是这样的。我在我的应用程序中有一个使用 $resource 的服务和一个 web api.

中的控制器

这是我在 angular

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

这是我的控制器

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");
        });
    };
};

在网络中 api 我创建了一个自定义方法

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


        return StatusCode(HttpStatusCode.NoContent);
    }

问题是我在 C# 中一直获取 garantia null 而我没有在 angular 中传递 null 对象。 是我的服务有问题还是c#有问题?

先谢谢你。

你应该这样调用方法:

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

我是这样解决的

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");
        });
    };
};

网络api:

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

     return StatusCode(HttpStatusCode.NoContent);
}