Get (Retrieve) CheckBox value using AngularJS in ASP.Net MVC

Last Reply 2 months ago By dharmendr

Posted 2 months ago

Hi,

I have one checkbox

By default i have checked that checkbox

if i am trying to save without touching that checkbox it was saving 0 value as per as my requirment i need to display 1 value

Could you please help me

@{ 
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.9/angular.min.js"></script>
<script>
    var app = angular.module('myApp', []);
    app.controller('myCtrl', function ($scope) {
        $scope.Save = function () {
            debugger;
            var LeaveType = { Active: $scope.Active };
            alert(LeaveType);
         }
    });
</script>
</head>
<body>
    <div ng-app="myApp" ng-controller="myCtrl">
        <input type="checkbox" ng-checked="true" ng-model="Active" />
        <div><input type="button" ng-click="Save()" value="Save" /></div>
    </div>
</body>
</html>

 

You are viewing reply posted by: dharmendr 2 months ago.
Posted 2 months ago

Hi mahesh213,

Inorder to get the value set the ng-model value for CheckBox on load.

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

HTML

<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) {
            $scope.Active = true; // Setting the CheckBox Checked.
            $scope.Save = function () {
                var LeaveType = { Active: $scope.Active ? 1 : 0 };
                alert(LeaveType.Active);
            }
        });
    </script>
</head>
<body>
    <div ng-app="MyApp" ng-controller="MyController">
        <input type="checkbox" ng-model="Active" />
        <input type="button" ng-click="Save()" value="Save" />
    </div>
</body>
</html>

Demo