Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to call my Mvc Controller Action from Angularjs controller $http.get(). Please provide some solution.

Controller Action :

 [HttpGet]
 public string CallMe(int number,string name)
 {
     return "Welcome " + name +" to Angular Js " + number + " times.";
 }

Angular JS Controller

 app.controller("StoreController", ["$http", function ($http) {        
    $http.get("/Home/CallMe", { number: 4, name: "angular"     }).success(function (data) {
        console.log(data);
    }).error(function (error) {           
        console.log(error);
    });
}]);

Also, Could anyone please specify related material for learning?

Thanks in advance.

share|improve this question
    
Please add the details of error that you are getting. –  Pramod Karandikar 19 hours ago
1  
Related material for learning: google.com –  user3036342 18 hours ago
    
@user3036342 Thanks.. But I have already googled it. it's my second day with the same problem. –  Ajay Verma 18 hours ago
    
@Pramod Karandikar The parameters dictionary contains a null entry for parameter 'number' of non-nullable type 'System.Int32' for method 'System.String CallMe(Int32, System.String)' in 'AngularJsdemo.Controllers.HomeController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.<br>Parameter name: parameters –  Ajay Verma 18 hours ago

2 Answers 2

up vote 1 down vote accepted

You are using $http#get method.

get(url, [config]);

 Param    Type     Details
 url      string   Relative or absolute URL specifying the destination of the request

 config   Object   Optional configuration object
(optional)

For passing parameters angular.http provides an option for it params

$http({
   url: "/Home/CallMe", 
   method: "GET",
   params: {number: 4, name: "angular"}
});
share|improve this answer
    
Thank you.. That's what i was looking for. Is there any other way to achieve that apart from #http#get? –  Ajay Verma 18 hours ago
    
@AjayVerma: You are welcome. Yes you can use POST method as well. Just need to change from method to post and from params to data and you are done. I don't know C# –  Aniket Kulkarni 18 hours ago

You're trying to use the get method with post parameters. get only takes a URL parameter, and an optional config one. Take a look here at the documentation.

Try to append your parameters to the URL:

 app.controller("StoreController", ["$http", function ($http) {        
    $http.get("/Home/CallMe?number=4&name=angular").success(function (data) {
        console.log(data);
    }).error(function (error) {           
        console.log(error);
    });
}]);
share|improve this answer
    
It worked. But that again arises a few question: 1. What if I want to send multiple data to the controller? 2. If it is Post; again if single parameter is required in Mvc Controller (with string name only). Mvc Controller get called with null value. How? What happened to [HttpGet]? e.g. public string CallMe(string name){} –  Ajay Verma 18 hours ago
    
@AjayVerma 1) Multiple data should either be still like this, or wrapped in a class and used in a post request. 2) string especially causes some kind of a problem in posts for some reason. You can either wrap it in a class, or you can try to use public string CallMe([FromBody] string name) if you're using WebAPI, that sometimes work –  Omri Aharon 18 hours ago
    
Thanks.. It also worked.. –  Ajay Verma 18 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.