Search code examples
angularjsmongodbasp.net-web-apimongodb-.net-driver

can't pass an array from angularjs service to web api


Problem is brandSelection value doesn't pass in the web api controller>>>

here is my $http.get() call from Angular Service:

   var _getItemByCategoryId = function (categoryId, currentPageNum, brandSelection) {

        var deferred = $q.defer();

        $http.get("Item/GetItemByCategoryId/categoryId/" + categoryId + "/currentPageNum/" + currentPageNum, { params: brandSelection }).success(deferred.resolve).error(deferred.reject);

        return deferred.promise;

};

Here is the WEBAPI code :

    [HttpGet]
    [Route("Item/GetItemByCategoryId/categoryId/{categoryId}/currentPageNum/{currentPageNum}")]
    public IHttpActionResult GetItemByCategoryId(string categoryId, int currentPageNum, string[] brandSelection)
    {
        var item = _itemService.GetItemByCategoryId(categoryId, currentPageNum);
        return Ok(item);
    }

The problem is that I get the value of categoryId and currentPageNum in this Web api controller but brandSelection value is always null.


Solution

  • Use [Formuri].

    [HttpGet]
    [Route("Item/GetItemByCategoryId/categoryId/{categoryId}/currentPageNum/{currentPageNum}")]
    public IHttpActionResult GetItemByCategoryId(string categoryId, int currentPageNum, [FromUri] string[] brandSelection)
    {
        var item = _itemService.GetItemByCategoryId(categoryId, currentPageNum);
        return Ok(item);
    }