【问题标题】:Is there a way to send an out parameter from a C# controller to an AngularJS JSON call?有没有办法将 C# 控制器的输出参数发送到 AngularJS JSON 调用?
【发布时间】:2017-12-02 08:20:03
【问题描述】:

我在 ASP.NET MVC 页面中使用 AngularJS 来创建数据网格。我让它与搜索和排序一起使用,现在我正在添加分页。为此,我需要记录记录数,以便知道在分页链接中显示多少页。

我的 C# 控制器有这个代码:

    public JsonResult GetJsonPeople(out int totalRecords, int pageNo = 1, string searchString = "", string sortColumn = "LastName", string sortDirection = "asc")
    {
        var ppl = db.People
            .Where(p => p.FirstName.Contains(searchString) || p.LastName.Contains(searchString))
            .Select(p => new {
                BusinessEntityID = p.BusinessEntityID,
                FirstName = p.FirstName,
                MiddleName = p.MiddleName,
                LastName = p.LastName,
                Suffix = p.Suffix,
                EmailPromotion = p.EmailPromotion,
                ModifiedDate = p.ModifiedDate })
            .OrderBy(sortColumn + " " + sortDirection)
            ;

        totalRecords = ppl.Count();

        if (pageSize > 0)
        {
            ppl = ppl.Skip((pageNo - 1) * pageSize).Take(pageSize);
        }
        // ViewBag.Search = search;

        var jsonResult = Json(ppl, JsonRequestBehavior.AllowGet);
        jsonResult.MaxJsonLength = int.MaxValue;
        return jsonResult;
    }

我的 AngularJS 调用如下所示:

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

PeopleApp.controller('peopleController', ['$scope', '$http', 
function ($scope, $http) {

getPeople();
function getPeople() {
    $http.get('/People/GetJsonPeople')
        .then(function(ppl) {
            $scope.people = ppl.data;
        });
}
$scope.searchPeople = function() {
    $http.get('/People/GetJsonPeople', {
        params:
        {
            "out totalRecords": $scope.totalRecords,
            "pageNo": $scope.pageNo,
            "searchString": $scope.searchText,
            "sortColumn": $scope.sortColumn,
            "sortDirection": $scope.sortDirection
        }
    })
        .then(function (ppl) {
            $scope.people = ppl.data;
        });

}
$scope.sortPeople = function (sortCol) {
    var sortDir;
    if (sortCol == $scope.sortColumn)
        sortDir = $scope.sortDirection == "asc" ? "desc" : "asc";
    else
        sortDir = "asc";
    $scope.sortDirection = sortDir;
    $scope.sortColumn = sortCol;

    $scope.searchPeople();
}

out 参数不起作用。它始终为零。你能在 Angular JSON 调用中设置一个 out 参数吗?如果没有,我如何才能将记录总数放入我的 Angular 应用程序模型中?

我想单独调用 $http.get 来获取记录数,但我不想给数据库带来不必要的调用负担。我尝试在控制器中设置一个类变量,在 C# 中设置它,然后从单独的 $http.get 返回它以返回计数的整数,但由于调用是异步的,第一个 $http.get 永远不会完成在第二个命中之前,所以它总是返回零。

我猜答案很简单,但我还没有得到答案。

【问题讨论】:

  • 我认为您需要了解 ASP.NET MVC 中的操作绑定是如何工作的,因为 out 参数不是这样工作的——它不可能工作(因为 HTTP 请求与本地进程内函数调用)。您需要在 DTO 或参数中单向传递所有内容。
  • 只需将其作为 JSON 有效负载的一部分返回即可。

标签: c# angularjs json asp.net-mvc


【解决方案1】:

不,您不能将 c# out 参数与 angularjs 调用(或任何 JavaScript 调用)一起使用。

但是您可以改为返回一个存储 ppl 数组和总数的对象,然后返回该对象

public JsonResult GetJsonPeople(int pageNo = 1, string searchString = "", string sortColumn = "LastName", string sortDirection = "asc")
{
    var ppl = db.People
        .Where(p => p.FirstName.Contains(searchString) || p.LastName.Contains(searchString))
        .Select(p => new {
            BusinessEntityID = p.BusinessEntityID,
            FirstName = p.FirstName,
            MiddleName = p.MiddleName,
            LastName = p.LastName,
            Suffix = p.Suffix,
            EmailPromotion = p.EmailPromotion,
            ModifiedDate = p.ModifiedDate })
        .OrderBy(sortColumn + " " + sortDirection)
        ;

    totalRecords = ppl.Count();

    if (pageSize > 0)
    {
        ppl = ppl.Skip((pageNo - 1) * pageSize).Take(pageSize);
    }
    // ViewBag.Search = search;
    var obj = new { ppl = ppl, totalRecords = totalRecords };
    var jsonResult = Json(obj, JsonRequestBehavior.AllowGet);
    jsonResult.MaxJsonLength = int.MaxValue;
    return jsonResult;
}

然后在客户端只从对象中获取值

$scope.searchPeople = function() {
$http.get('/People/GetJsonPeople', {
    params:
    {
        "pageNo": $scope.pageNo,
        "searchString": $scope.searchText,
        "sortColumn": $scope.sortColumn,
        "sortDirection": $scope.sortDirection
    }
    })
    .then(function (result) {
        $scope.totalRecords = result.data.totalRecords;
        $scope.people = result.data.ppl;
    });
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-03-01
    • 1970-01-01
    • 2012-05-13
    • 1970-01-01
    • 1970-01-01
    • 2017-01-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多