I want to use AngularJs functionality in a view where i want to fetch records and add records i am using following code but i am not getting expected result.
<div ng-app="MyApp" ng-controller="MyController">
<table id="tblCustomers" class="table" cellpadding="0" cellspacing="0">
<tr>
<th style="width:100px">Customer Id</th>
<th style="width:150px">Name</th>
<th style="width:150px">Country</th>
<th style="width:100px"></th>
</tr>
<tbody ng-repeat="m in Customers">
<tr>
<td><span>{{m.CustomerId}}</span></td>
<td>
<span ng-hide="m.EditMode">{{m.Name}}</span>
<input type="text" ng-model="m.Name" ng-show="m.EditMode" />
</td>
<td>
<span ng-hide="m.EditMode">{{m.Country}}</span>
<input type="text" ng-model="m.Country" ng-show="m.EditMode" />
</td>
<td>
@*<a class="Edit" href="javascript:;" ng-hide="m.EditMode" ng-click="Edit($index)">Edit</a>
<a class="Update" href="javascript:;" ng-show="m.EditMode" ng-click="Update($index)">Update</a>
<a class="Cancel" href="javascript:;" ng-show="m.EditMode" ng-click="Cancel($index)">Cancel</a>
<a href="javascript:;" ng-hide="m.EditMode" ng-click="Delete(m.CustomerId)">Delete</a>*@
</td>
</tr>
</tbody>
</table>
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td style="width: 150px">
Name<br />
<input type="text" ng-model="Name" style="width:140px" />
</td>
<td style="width: 150px">
Country:<br />
<input type="text" ng-model="Country" style="width:140px" />
</td>
<td style="width: 200px">
<br />
<input type="button" value="Add" ng-click="Add()" />
</td>
</tr>
</table>
</div>
<script type="text/javascript">
var app = angular.module('MyApp', [])
app.controller('MyController', function ($scope, $http, $window) {
//Getting records from database.
var post = $http({
method: "GET",
url: "/Customer/GetCustomers",
dataType: 'json',
headers: { "Content-Type": "application/json" }
});
post.success(function (data, status) {
//The received response is saved in Customers array.
$scope.Customers = data;
});
//Adding new record to database.
$scope.Add = function () {
if (typeof ($scope.Name) == "undefined" || typeof ($scope.Country) == "undefined") {
return;
}
var post = $http({
method: "POST",
url: "/Customer/InsertCustomer",
data: "{name: '" + $scope.Name + "', country: '" + $scope.Country + "'}",,
dataType: 'json',
headers: { "Content-Type": "application/json" }
});
post.success(function (data, status) {
//The newly inserted record is inserted into the Customers array.
$scope.Customers.push(data)
});
$scope.Name = "";
$scope.Country = "";
};
});
</script>
Here is my controller code in first method i am getting all records in debugging mode i am getting all customers and in saving method i am not getting inserted text data.
public JsonResult GetCustomers()
{
var Customers = db.Query<Customer>("Select * from Customer");
return Json(Customers);
}
[HttpPost]
public JsonResult InsertCustomer(string name, string country)
{
Customer customer = new Customer();
customer.Name = name;
customer.Country = country;
db.Save(customer);
return Json(customer);
}
I am neither getting any records nor able to insert any records.Can anybody help me with whats going wrong


