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

I have tried to pass the customer class object to Asp.net MVC controller using angularJS $http service.

var app = angular.module("myApp", []);
app.controller("myCtrl", function ($scope,$http)
{   
    $scope.getRecord = function () {

        $scope.objCustomer = { Id: 5, Name: 'sasi', Dept: 'IT' };               
        $http({ url: "Home/GetCustbyID", 
                method: "GET", 
                params: {objCustomer: $scope.objCustomer} })
        .then(function (response) 
            {    
             //success code
            };});
}});

Controller Action is defined like:

public JsonResult GetCustbyID(Customer objCustomer)
{
   return Json(objCustomer, JsonRequestBehavior.AllowGet);
}

However in the above controller action customer object is always passed as null. Did i miss anything?

Please help me guys to resolve this.

share|improve this question
2  
If you want to send multiple params in a JSON object, you should use POST instead of GET – Arkantos Jan 8 at 9:49
    
Post your Controller "Home/GetCustbyID" code. If it is a get by Id, why do you need Name and Dept? – jpgrassi Jan 8 at 9:58
    
Thanks for the quick response @Arkantos. Yeah !! Its working fine after I changed it to POST method. Thanks :) – sasi Jan 8 at 10:06

You should use a POST request if you wish to send multiple parameters at once in a JSON object in your AJAX request and capture that as an Object on your server side.

$http.post(url, $scope.objCustomer)
     .then(successCallback, errorCallback); 
share|improve this answer

As you are actually sending data to server you are posting in $http.

var app = angular.module("myApp", []);
app.controller("myCtrl", function ($scope,$http)
{   
    $scope.getRecord = function () {

        $scope.objCustomer = { Id: 5, Name: 'sasi', Dept: 'IT' };               
        $http({ 
                url: "Home/GetCustbyID", 
                method: "POST", 
                data:  $.param({ objCustomer : $scope.objCustomer })                
              })
        .then(function(response) {    
             //success code
            };});
}});

in other case if you like to send/pass data as json string you will have to use stringify like this

 var data = $.param({
            json: JSON.stringify({
                name: $scope.name
            })
        });
 $http.post("/path_of_controller", data).success(function(data, status) {
           //do whatever you like
 })
share|improve this answer
    
Yes..! What you posted above is correct. This below code is works for me after i changed it to POST method. $http({ url: "Home/GetCustbyID", method: "POST", data: { objCustomer: $scope.objCustomer} }). then(function (response){});}); But I used 'data' instead of params keyword. – sasi Jan 8 at 10:18

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.