【问题标题】:Angular JS Json dataAngular JS Json 数据
【发布时间】:2017-08-09 04:56:20
【问题描述】:

我在 Angular js 中自学。为此,我创建了一个以我工作中的实际项目为模型的项目

一切正常,但 POST 出现问题

我的控制器.js

var ngapp = angular.module('ngWebApp', ['angularUtils.directives.dirPagination']);
ngapp.controller('ngIndexController', function ($scope, ngDashboardService) {
 $scope.exportData = function (selectedDataList) {

    var getData = ngDashboardService.AddReportAudit(selectedDataList)
    getData.then(function (result) {
            alert(result.data);
        }, function () {
            alert('Error in getting records');
        });        
    };
});

我的服务.js

angular.module('ngWebApp').service("ngDashboardService", function ($http) {
    this.AddReportAudit = function (dataList) {
        var response = $http({
            method: "POST",
            url: "/Home/AddReportAudit",
            data: {
            'dataList': JSON.stringify(dataList)
            }
        });
        return response;
    };
});

我在 HomeController 中的 JasonResult 代码

 public JsonResult AddReportAudit(List<ADTOWebSMARTT_SSOData> dataList)
 {
    if (dataList != null)
    {
        using (HRMSystemEntities contextObj = new HRMSystemEntities())
        {
            var itemList = dataList.Where(x => x.IsChecked == true).ToList();
            itemList.ForEach(a => a.DateChecked = DateTime.Now);
            contextObj.SaveChanges();
            return Json(new { success = true });
        }
    }
    else
    {
        return Json(new { success = false });
    }
}

问题出现在这里 公共 JsonResult AddReportAudit(列出数据列表) 由于某种原因,到达 AddReportAudit 的 dataList 变为空,即列表的元素为零。 dataList 在 controller.js 和 service.js 中有 30 条记录。

我不确定为什么会这样。当 json 数据来自 angular 到 c# 时,我是否缺少解析

【问题讨论】:

  • 您是否尝试将 [FromBody] 放在 AddReportAudit 的列表参数前面?此外,将 contentType:'application/json' 和 dataType:'json' 放入您的角度请求中。

标签: javascript c# angularjs json model-view-controller


【解决方案1】:

角度解决方案 1. 放置您的 Web 服务 url 并根据需要更改 json 数据格式。它有效。

<!DOCTYPE html>
<html>
<head>
    <title>Angular HTTP service</title>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
</head>
<body ng-app="myApp" ng-controller="myCtrl">
    <table>
        <tr>
            <th>S No</th>
            <th>Source Name</th>
        </tr>
        <tr ng-repeat="res in result">
            <td>{{$index + 1}}</td>
            <td>{{res.source_name}}</td>
        </tr>


    </table>
    <script type="text/javascript">
        var app = angular.module("myApp", []);
        app.controller("myCtrl", function($scope, $http){
            $http.get("http://127.0.0.1:4000/all_source").then(function(res){
                //console.log(res.data.result);
                //console.log(res);
                $scope.result = res.data.result;
            });
        });
    </script>
</body>
</html>

【讨论】:

  • 请阅读this 元问题并确保您下次不要包含图片,而是文字本身。
【解决方案2】:

您实际上是在发送一个对象,因此到达您的公共 JsonResult AddReportAudit(....isAnObject) 的数据,但您期望它是一个列表。只需将您的控制器代码更改为下面的 sn-p,它应该可以工作。

angular.module('ngWebApp').service("ngDashboardService", 
    function($http) {
       this.AddReportAudit = function (dataList) {
          var response = $http({
             method: "POST",
             url: "/Home/AddReportAudit",
             data:JSON.stringify(dataList)
          });
       return response;
    };
});

【讨论】:

  • 你我的朋友是个天才
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-07-02
  • 2013-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多