【问题标题】:How to pass AngularJS variable value to C# controller?如何将 AngularJS 变量值传递给 C# 控制器?
【发布时间】:2019-06-02 21:28:58
【问题描述】:

我想从 AngularController.js 获取 angularjs 值到 CCharpController.cs

我想将 $scope.count1 和 $scope.count2 的值相加

`

$scope.onSubmit = function () {
    for (var i = 0 ; i < $scope.Data.length ; i++) {
        if ($scope.Data[i].1 === true) {
            $scope.count1 += 1;
        }
        else if ($scope.Data[i].2 === true) {
            $scope.count2 += 1;
        }
    }
}

`

并像这样传递给 CCharp 函数,

`

[Route("api/WarehousePlan/AllowWarehousePlan")]
[HttpPost]
[Authorize]
public IHttpActionResult AllowWarehousePlan([FromBody] WarehouseAllowDenyModel model){ 
    ....
    var count = $scope.count1 + $scope.count2 }

`

我希望从 angularjs 函数接收计数值。

【问题讨论】:

标签: c# angularjs asp.net-web-api


【解决方案1】:

首先声明一个模型

$scope.modelVM = {};

然后按照您在模型类中声明的方式将参数分配给模型

 $scope.modelVM.count1 += 1;
 $scope.modelVM.count2 += 1;

然后通过http发布模型

 $http.post('api/WarehousePlan/AllowWarehousePlan', $scope.modelVM).then(
            function (successResponse) {
                if (successResponse.status == '200') {
                    alert("Data Submit Successfully..");
                }
            },
            function (errorResponse) {
                alert("Not Successful");
            });

【讨论】:

    【解决方案2】:

    在Controller文件的Cookie中设置值,

     HttpContext.Current.Response.Cookies["count1"].Value = "1";
    

    你可以访问JS文件中的cookie值,

    $scope.count1 = $cookies.get("count1");
    

    更新JS文件中的Cookie值,

    $cookies.put('count1',2);
    

    【讨论】:

      猜你喜欢
      • 2014-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多