CodeDada
CodeDada

Reputation: 435

How do I pass an array of strings to C# web api from AngularJS?

data.js

My method call:

var dateCollection = ["2014-12-12,"2013-12-12"];
getCompanyData(1,dateCollection);

var getCompanyData = function (Id, stmtDate)
        {
            var promise = $http.get(baseUrl() + "api/Search/CompanyData/" + Id + "/" + stmtDate)
                .success(function (data, status, headers, config) {

                    return data;
                })
                .error(function (data, status, headers, config) {
                    return data;
                });

            return promise;

        }

SearchController.cs

[ActionName("CompanyData")]
[HttpGet]
public async Task<IHttpActionResult> GetCompanyData(string Id , string[] stmtDate)
{
}

I need to send array of stmtDate (which contains strings) to a GetCompanyData web API controller.

My WebApiConfig.cs has following route:

config.Routes.MapHttpRoute(
                name: "ApiByMultiParams",
                routeTemplate: "api/{controller}/{action}/{Id}/{stmtDate}"
            );

The problem, is while trying to pass an array of data, when it hits the web API controller method: GetCompanyData, stmtDate doesn't receive the array and it is coming up as null. Also iI would appreciate any suggestions on how to convert a datetime which is in the format: 2014-12-12 00:00:00 to 2014-12-12 (in AngularJS).

Upvotes: 2

Views: 1004

Answers (1)

aseferov
aseferov

Reputation: 6403

you are trying add array at end of url string. Send date as parametr

var getCompanyData = function (Id, stmtDate) {
    var promise = $http.get(baseUrl() + "api/Search/CompanyData/" + Id, {
            params: {
                date: stmtDate
            }
        }
    ).success(function (data, status, headers, config) {

            return data;
        })
        .error(function (data, status, headers, config) {
            return data;
        });

    return promise;

}

Upvotes: 1

Related Questions