0

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
]
4
  • 1
    self.Employees.push(data); should be self.Employees(data);, try that way. And you dont need - self.EmpId = ko.observable(""); self.Name = ko.observable(""); self.City = ko.observable(""); Commented Jun 22, 2015 at 11:09
  • What is the data that is returned from a call to "/Employee/About"? Can you add a sample? Commented Jun 22, 2015 at 11:09
  • @ramiramilu That worked. Previuosly i have taken the same Observable array but i have pushed and it worked Commented Jun 22, 2015 at 11:10
  • Glad I am helpful. I posted the same as answer. Commented Jun 22, 2015 at 11:25

1 Answer 1

3

self.Employees.push(data); should be self.Employees(data);

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.