简体   繁体   中英

Posting JSON objects into MVC Controller C#

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM