-1

JS File

var module = angular.module('myApp', []);

module.controller('myController', function ($scope, $http) {
    $scope.items = [{}];
    $scope.users = {};
    $scope.addfield = function () {
        $scope.items.push({});
    }
    $scope.saveUser = function () {
        $scope.results = [];
        console.log($scope.users);

        $http({
            method: 'POST',
            url: '/Data/MethodTest',

            data:{
                users: $scope.users
            }

        }).success(function (data) {
            console.log(data)
        });

    }

});

HTML Code

<body data-ng-app="myApp" data-ng-controller="myController">
    <form name="educdetailsform" novalidate>
        <label>Education Details</label></br>
        <button ng-click="addfield()">Add Education</button>
        <div ng-repeat="item in items track by $index">
            <input type="text" name="empData.qualification[]" placeholder="Qualification" ng-model="users[$index].qualification">
            <input type="text" name="empData.year[]" placeholder="Year of Passing" ng-model="users[$index].year">
            <input type="text" name="empData.percentage[]" placeholder="Percentage" ng-model="users[$index].percentage">
        </div>
        <input type="submit" name="submit" ng-click="saveUser()" />
    </form>
    </div>
</body>

.cs Code // I'm trying to insert values of the object fetched into database. Before which I've just written code to deserialise the objects. as of now I'm getting just null values passed from the JS//

 public JsonResult MethodTest(string jsonData)
         {
             string jdata = jsonData.ToString();
             List<StockAllocate> stockData;
             bool status = false;


                JavaScriptSerializer jss = new JavaScriptSerializer();
                stockData = jss.Deserialize<List<StockAllocate>>(jdata);
                 status = true;


             return new JsonResult { Data = new { status = status } };
         }
Test
  • 1
  • 3
  • refer [this thread](http://stackoverflow.com/questions/19254029/angularjs-http-post-does-not-send-data) – Keppy Oct 13 '16 at 05:05

2 Answers2

0

By default only complex parameters are resolved from body. You need to add [FromBody] prefix for string.

public JsonResult MethodTest([FromBody]string jsonData)

But in case if you don't know:

public JsonResult MethodTest(List<StockAllocate> jsonData)

is also possible.

Sefa
  • 8,865
  • 10
  • 51
  • 82
  • [HttpPost] public JsonResult MethodTest(List jsonData) { bool status = false; using (InventoryonlineEntities dc = new InventoryonlineEntities()) { foreach (var e in jsonData) { dc.StockAllocates.Add(e); dc.SaveChanges(); } status = true; } return new JsonResult { Data = new { status = status } }; } – Test Oct 13 '16 at 06:54
  • I've done the above changes. Still JSON passes null values – Test Oct 13 '16 at 06:55
0

Try renaming your input parameter in CS Controller, from "jsonData" to "users" (the one you've used in your JS $scope.saveUser's $http.post data.

Else, try this below code:

$http.post('/Data/MethodTest/', {users: $scope.users})
.success(function(data){
  // Code here
})
.error(function(result){
 // code here
});

Also don't forget to decorate your CS function MethodTest with [HttpPost].

[HttpPost]    
public JsonResult MethodTest(string users)
             {
                 string jdata = users.ToString();
                 List<StockAllocate> stockData;
                 bool status = false;


                    JavaScriptSerializer jss = new JavaScriptSerializer();
                    stockData = jss.Deserialize<List<StockAllocate>>(jdata);
                     status = true;


                 return new JsonResult { Data = new { status = status } };
             }

Hope this helps. Thanks.

Denver Gomes
  • 44
  • 1
  • 12
  • I changed public JsonResult MethodTest(string users) and also the JS code. Still there is no benefit. – Test Oct 13 '16 at 07:11
  • @Test: You may please refer to his post - http://techfunda.com/howto/565/http-post-server-request. This will definitely help you. Thanks. – Denver Gomes Oct 14 '16 at 05:09