1

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.

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

2 Answers 2

0

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); 
Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.