Pass (Send) value from View to Controller using AngularJS in ASP.Net MVC

Last Reply 20 days ago By dharmendr

Posted 23 days ago

I have one script like below i have hardcode some value

currently my requirement is that how to pass this values from js to controller

Note: after loading i need to load this below script without clicking of any change event and also i need pass value only from js to controller

If you can look at into view I have 2 buttons

curently requirement is that based upon FLI_code I am going to show buttons on screen

I am going to hardcode FLI_code on script

could you please help me

        GetAllRights();
        function GetAllRights() {
            debugger;
            var getData = myService.getrights(138);
            var getData = myService.getrights();
            getData.then(function (rt) {
                $scope.rights = rt.data;
            }, function (rt) {
                alert("Records gathering failed!");
            });
        }

    //get all rights
    this.getrights = function () {
        debugger;
        var response = $http({
            method: "post",
            url: "/general/getRights",
            params: { FLI_Code: JSON.stringify(Id) }
        });
        return $http.get("/employee/getRights");
    };

View

<div ng-repeat="right in rights">
    <button ng-if="right.UAR_Add==1" ng-show="addplus" >Add</button>
    <button  ng-if="right.UAR_Edit==1" ng-show="editminus">Edit</button>
</div>

 

[HttpGet]
//for fetching all rights     
public JsonResult getRights(string FLI_Code)
{
}

 

Posted 20 days ago

Hi mahesh213,

Check this example. Now please take its reference and correct your code.

Controller

public class HomeController : Controller
{
    // GET: /Home/
    public ActionResult Index()
    {
        return View();
    }

    [HttpGet]
    public JsonResult getRights(string FLI_Code)
    {
        List<Right> right;
        if (FLI_Code == "138")
        {
            right = new List<Right>();
            right.Add(new Right { Id = 1, UAR_Add = 1, UAR_Edit = 0 });
            right.Add(new Right { Id = 2, UAR_Add = 0, UAR_Edit = 1 });
        }
        else
        {
            right = new List<Right>();
            right.Add(new Right { Id = 1, UAR_Add = 0, UAR_Edit = 1 });
            right.Add(new Right { Id = 2, UAR_Add = 1, UAR_Edit = 0 });
        }
        return Json(right, JsonRequestBehavior.AllowGet);
    }

    public class Right
    {
        public int Id { get; set; }
        public int UAR_Add { get; set; }
        public int UAR_Edit { get; set; }
    }
}

View

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Index</title>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
    <script type="text/javascript">
        var app = angular.module('MyApp', []);
        app.controller('MyController', function ($scope, $http) {
            $scope.GetAllRights = function () {
                var Id1 = 138
                $http({
                    method: 'get',
                    url: '/Home/getRights/',
                    params: { FLI_Code: JSON.stringify(Id1) }
                }).then(function (data) {
                    $scope.rights = data.data;
                }, function (response) {
                    alert("Records gathering failed!");
                });
            }
        });
    </script>
</head>
<body>
    <div ng-app="MyApp" ng-controller="MyController">
        <input type="button" value="Save" ng-click="GetAllRights()" /><br /><br />
        <div ng-repeat="right in rights">
            <button ng-if="right.UAR_Add==1">Add</button>
            <button ng-if="right.UAR_Edit==1">Edit</button>
            <hr />
        </div>
    </div>
</body>
</html>

Screenshot