简体   繁体   English

如何将AngularJS变量值传递给C#控制器?

[英]How to pass AngularJS variable value to C# controller?

I want to get angularjs value from AngularController.js to CCharpController.cs 我想从AngularController.js到CCharpController.cs获取angularjs值

This I want to sum value from $scope.count1 and $scope.count2 我想对$ 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;
        }
    }
}

` `

and pass to CCharp function like this, 然后传递给CCharp函数,

` `

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

` `

I expect receive count value from angularjs function. 我希望从angularjs函数接收计数值。

First declare a model 首先声明一个模型

$scope.modelVM = {};

Then assign the parameter into model as likes you declare in your model class 然后像在模型类中声明的那样将参数分配给模型

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

After then post the model via http 之后,通过http发布模型

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

Set value in Cookie in Controller file, 在Controller文件中的Cookie中设置值,

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

You can access cookie value in JS file, 您可以访问JS文件中的cookie值,

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

To Update Cookie value in JS file, 要更新JS文件中的Cookie值,

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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