I am trying to display all the employees in the database and I am unable to achieve it.
My JS,
var EmployeeKoViewModel = function () {
var self = this;
self.EmpId = ko.observable("");
self.Name = ko.observable("");
self.City = ko.observable("");
self.Employees = ko.observableArray([]);
GetEmployees();
function GetEmployees() {
$.ajax({
type: "GET",
url: "/Employee/About",
}).done(function (data) {
self.Employees.push(data);
}).error(function (ex) {
alert("Error");
});
}
}
$(document).ready(function myfunction() {
ko.applyBindings(new EmployeeKoViewModel());
})
And my View,
<form>
<table>
<tr>
<td>
<div class="FixedContainer">
<table data-bind="visible: Employees().length>0" style="border: double">
<thead>
<tr>
<td>EmpId</td>
<td>Name</td>
<td>City</td>
<td></td>
</tr>
</thead>
<tbody data-bind="foreach: Employees">
<tr>
<td data-bind="text: EmpId"></td>
<td data-bind="text: Name"></td>
<td data-bind="text: City"></td>
</tr>
</tbody>
</table>
</div>
</td>
</tr>
</table>
</form>
There is no data showing up in UI .I have checked console in browser but no errors.
Can you guide Where I am doing wrong please.
data
[
ObjectCity: "Hyderabad"
EmpId: 1Name: "Vivek"
__proto__: Object
]
self.Employees.push(data);should beself.Employees(data);, try that way. And you dont need -self.EmpId = ko.observable(""); self.Name = ko.observable(""); self.City = ko.observable("");"/Employee/About"? Can you add a sample?