Sunday, March 26, 2017

Difference between SingleOrDefault() n FirstOrDefault()

Whenever you use SingleOrDefault, you clearly state that the query should result in at most a single result. On the other hand, when FirstOrDefault is used, the query can return any amount of results but you state that you only want the first one.
I personally find the semantics very different and using the appropriate one, depending on the expected results, improves readability.

AngularJS ASP.NET MVC Editable Grid

Model-------------
public class Employee
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Country { get; set;
   }

View......
<link href="../../style/bootstrap.min.css" rel="stylesheet" type="text/css" />
<script src="../../script/angular.js" type="text/javascript"></script>
<script type="text/javascript">
    var app = angular.module('myApp', []);
    app.controller('HomeCtrl', ['$scope', '$http', function ($scope, $http) {

        $http({
            method: 'POST',
            url: 'Home/GetEmployees'
        })
        .then(function (response) {
            console.log(response);
            $scope.employees = response.data.employees;

            angular.forEach($scope.employees, function (obj) {
                obj["showEdit"] = true;
            });

        }, function (error) {
            console.log(error);
        });

        $scope.toggleEdit = function (emp) {
            emp.showEdit = emp.showEdit ? false : true;
        }
    } ]);
</script>
<div ng-app="myApp" ng-controller="HomeCtrl" class="container">
    <div class="row">
        <div class="bs-example marginTop50" data-example-id="table-within-panel">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h3>Code Handbook</h3></div>
                <div class="panel-body">
                    <p>
                        AngularJS ASP.NET MVC Editable Grid Demo</p>
                </div>
                <table class="table">
                    <thead>
                        <tr>
                            <th>
                                #
                            </th>
                            <th>
                                First Name
                            </th>
                            <th>
                                Last Name
                            </th>
                            <th>
                                Country
                            </th>
                            <th>

                            </th>

                        </tr>
                    </thead>
                    <tbody>
                        <tr ng-repeat="emp in employees">
                            <th scope="row">
                                {{$index+1}}
                            </th>
                            <td>
                                <span ng-show="emp.showEdit">{{emp.FirstName}}</span>
                                <input ng-show="!emp.showEdit" type="text" ng-model="emp.FirstName" class="form-control" >
                            </td>
                            <td>
                                 <span ng-show="emp.showEdit">{{emp.LastName}}</span>
                                 <input ng-show="!emp.showEdit" type="text" ng-model="emp.LastName" class="form-control" >
                            </td>
                            <td> <span ng-show="emp.showEdit">{{emp.Country}}</span>
                                <input ng-show="!emp.showEdit" type="text" ng-model="emp.Country" class="form-control" >
                            </td>
                            <td>
                                <span ng-show="emp.showEdit" ng-click="toggleEdit(emp)" class="glyphicon glyphicon-pencil"></span>
                                <span ng-show="!emp.showEdit" ng-click="toggleEdit(emp)" class="glyphicon glyphicon-ok"></span>
                            </td>
                        </tr>

                    </tbody>
                </table>

            </div>
        </div>
    </div>

</div>
Controller----
public JsonResult GetEmployees()
        {
            List<Employee> empList = new List<Employee>();

            Employee emp = new Employee { FirstName = "James", LastName = "Bond", Country = "Germany" };
            empList.Add(emp);

            emp = new Employee { FirstName = "Roy", LastName = "Agasthyan", Country = "United States" };
            empList.Add(emp);

            return Json(new { employees = empList });
        }



Wednesday, March 8, 2017

Disable browser right click and F12 keyboard codes javascript

<body oncontextmenu="return disableRightClick();">
<script type="text/javascript">
    function disableRightClick() {
        //alert("Sorry, right click is not allowed !!");
        return false;
    }

    document.onkeypress = function(event) {
        event = (event || window.event);
        if (event.keyCode == 123) {
            return false;
        }
    };
    document.onmousedown = function(event) {
        event = (event || window.event);
        if (event.keyCode == 123) {
            return false;
        }
    };
    document.onkeydown = function (event) {
        event = (event || window.event);
        if (event.keyCode == 123) {
            return false;
        }
        if ((event.ctrlKey && (event.keyCode === 85 || event.keyCode === 117)) || (event.ctrlKey && event.shiftKey && event.keyCode === 73) || (event.ctrlKey && event.shiftKey && event.keyCode === 67)) {
            //alert('not allowed');
            return false;
        } else {
            return true;
        }
    }


</script>
Key Code
backspace 8
tab 9
enter 13
shift 16
ctrl 17
alt 18
pause/break 19
caps lock 20
escape 27
(space) 32
page up 33
page down 34
end 35
home 36
left arrow 37
up arrow 38
right arrow 39
down arrow 40
insert 45
delete 46
0 48
1 49
2 50
3 51
4 52
5 53
6 54
7 55
8 56
9 57
a 65
b 66
c 67
d 68
Key Code
e 69
f 70
g 71
h 72
i 73
j 74
k 75
l 76
m 77
n 78
o 79
p 80
q 81
r 82
s 83
t 84
u 85
v 86
w 87
x 88
y 89
z 90
left window key 91
right window key 92
select key 93
numpad 0 96
numpad 1 97
numpad 2 98
numpad 3 99
numpad 4 100
numpad 5 101
numpad 6 102
numpad 7 103
Key Code
numpad 8 104
numpad 9 105
multiply 106
add 107
subtract 109
decimal point 110
divide 111
f1 112
f2 113
f3 114
f4 115
f5 116
f6 117
f7 118
f8 119
f9 120
f10 121
f11 122
f12 123
num lock 144
scroll lock 145
semi-colon 186
equal sign 187
comma 188
dash 189
period 190
forward slash 191
grave accent 192
open bracket 219
back slash 220
close braket 221
single quote 222