Join the Stack Overflow Community
Stack Overflow is a community of 6.4 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

i'm trying to pass object parameter from angularJs controller to Web Api 2 Get Method controller.

If i pass object like string it will work but i don't like this solution.

[HttpGet]
public IEnumerable<SearchVenueByParameters_Result> Get(string parameter)
    {
        ObjSearch obj_search = new JavaScriptSerializer().Deserialize<ObjSearch>(parameter);

I would like to try this one :

[HttpGet]
    public IEnumerable<SearchVenueByParameters_Result> Get(ObjSearch obj_search)

ObjSearch class :

public class ObjSearch {
    public string aAMSID { get; set; }
    public string venueName { get; set; }
    public string address { get; set; }
    public byte? businessSK { get; set; }
    public string subjectAamsCode { get; set; }
    public string subjectDenomination { get; set; }
    public string subjectVatNumber { get; set; }
    public string subjectTaxCode { get; set; } }

And in angularjs controller i have :

 $scope.objSearch = {
        "aAMSID": "",
        "venueName": "",
        "address": "",
        "businessSK": "",
        "subjectAamsCode": "",
        "subjectDenomination": "",
        "subjectVatNumber": "",
        "subjectTaxCode": ""
        }

$scope.searchVenueByParameters = function () {

    var strObjSearch = angular.toJson($scope.objSearch);
    console.log(strObjSearch);
    $http({
        url: '/api/SearchVenueByParameters_Result/',
        method: 'GET',
        params: { obj_search: strObjSearch }
    }).success(function (data) {
        $scope.Venues = data;
        $scope.VenueList = true;
        $scope.showItem = true;

})
   .error(function () {
       $scope.error = "An Error has occured while loading posts!";
   });
    }

But in this case i have corrected value printed in console from angularjs controller , but the object passed in the web abi controller is always null.

share|improve this question
up vote 0 down vote accepted

When sending complex objects with GET use the [FromUri] attribute which creates your object directly from the URL.

In the client, add the object to the params

$http({
    url: '/api/SearchVenueByParameters_Result/',
    method: 'GET',
    params: $scope.objSearch
}).success(function (data) {
    $scope.Venues = data;
    $scope.VenueList = true;
    $scope.showItem = true;

The URL will be translated to something like

/api/SearchVenueByParameters_Result/?aAMSID=1&venueName=1&address=1&businessSK=1&subjectAamsCode=1&subjectDenomination=1&subjectVatNumber=1&subjectTaxCode=1

And in the controller, add the [FromUri] attribute

[HttpGet]
public IEnumerable<SearchVenueByParameters_Result> Get([FromUri]ObjSearch obj_search)

You can read more about the attribute here

share|improve this answer
    
Can i also use FromBody? Because it's not working – Francesco Galeota 17 hours ago
    
@FrancescoGaleota GET request are design not to have a body. So the [FromBody] will not work with GET. – Marcus H 16 hours ago

You should be using Post to send objects to WEB API. Replace your code with these

 $scope.objSearch = {
        "aAMSID": "",
        "venueName": "",
        "address": "",
        "businessSK": "",
        "subjectAamsCode": "",
        "subjectDenomination": "",
        "subjectVatNumber": "",
        "subjectTaxCode": ""
        }

$scope.searchVenueByParameters = function () {

    var strObjSearch = angular.toJson($scope.objSearch);
    console.log(strObjSearch);
    $http({
        url: '/api/SearchVenueByParameters_Result/',
        method: 'POST',
        params: { obj_search: strObjSearch }
    }).success(function (data) {
        $scope.Venues = data;
        $scope.VenueList = true;
        $scope.showItem = true;

})
   .error(function () {
       $scope.error = "An Error has occured while loading posts!";
   });
    }

[HttpPost]
public IEnumerable<SearchVenueByParameters_Result> Get(string parameter)
    {
        ObjSearch obj_search = new JavaScriptSerializer().Deserialize<ObjSearch>(parameter);
share|improve this answer
    
I need to Get information , not Post .Not working – Francesco Galeota 17 hours ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.