0

I have this service

  this.getyear = function (bookId) {
            alert();
            var response = $http({
                method: "post",
                url: "Dashboard/GetyearId",
                data: JSON.stringify(12,14),
                dataType: "json"
            });
            return response;
        }

this is my controller

controller('myController', function ($scope, AttendanceService) {
  var getyear = AttendanceService.getyear();
            getyear.then(function (_book) {

            })
});

and this is my actionresult method

 public ActionResult GetyearId(string Yearid)
    {
        return Json(Yearid, JsonRequestBehavior.AllowGet);
    }

I want to pass 2-3 ids to actionresult and retrive it over there.How can it be possible? I am very new to angularJS .Thanks in advance

akash
  • 173
  • 1
  • 14

2 Answers2

1

I think the service could be something like:

//service
this.getyear = function (bookId) {

    return $http({
        method: "post",
        url: "Dashboard/GetyearId",
        data: JSON.stringify([12, 13, 14]) //for three ids
    });
}

//and controller:
controller('myController', function ($scope, AttendanceService) {
    AttendanceService.getyear()
        .then(function (_book) {

        })
});
David Tao
  • 513
  • 5
  • 12
  • But then how to retrieve it on server side actionresult method?? – akash Apr 20 '16 at 04:34
  • I am getting string Yearid value null on server side – akash Apr 20 '16 at 04:36
  • @akash Sorry I'm not so familiar with asp.net, I use php as the server-side language – David Tao Apr 20 '16 at 08:16
  • @akash I've done some other research, please have a look at this link http://victorblog.com/2012/12/20/make-angularjs-http-service-behave-like-jquery-ajax/, I used to have similar issue on php, but I'm not sure if that could happen to asp.net – David Tao Apr 20 '16 at 11:22
  • @akash and I also found another post on stackoverflow http://stackoverflow.com/questions/19254029/angularjs-http-post-does-not-send-data, this guy have similar issue as you have with asp.net, hope this can help :) – David Tao Apr 20 '16 at 11:29
0

I prefer calling like this -

this.getyear = function (bookId) {
     var url =  "Dashboard/GetyearId",
     var data =  [12,10]; //A json object
            //or
     var data =  JSON.stringify([10,14]) //in your case, but better pass and receive objects

     return $http.post(url, data);
}
VISHAL DAGA
  • 4,132
  • 10
  • 47
  • 53