[英]Posting JSON objects into MVC Controller C#
JS File JS文件
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 HTML代码
<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. .cs 代码 // 我正在尝试将提取到数据库中的对象的值插入。 Before which I've just written code to deserialise the objects.在此之前,我刚刚编写了反序列化对象的代码。 as of now I'm getting just null values passed from the JS//到目前为止,我只得到了从 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.您需要为字符串添加 [FromBody] 前缀。
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.尝试将 CS Controller 中的输入参数从“jsonData”重命名为“users”(您在 JS $scope.saveUser
的$http.post
数据中使用的$http.post
。
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] 装饰你的 CS 函数 MethodTest。
[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.谢谢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.