Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
displaying json data in browser knockout.js in mvc4

SQL
in controller
public JsonResult FetchEmployees()
       {
          return Json(db.EmployeeInfoes.ToList(), JsonRequestBehavior.AllowGet);

       }

employeelistvm.js



var urlPath = window.location.pathname;
$(function () {
    ko.applyBindings(EmployeeListVM);
    EmployeeListVM.getEmployees();
});

//View Model
var EmployeeListVM = {
    Employees: ko.observableArray([]),
    getEmployees: function () {
        var self = this;
        $.ajax({
            type: "GET",
            url: '/EmployeeInfo/FetchEmployees',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {
                self.Employees(data); //Put the response in ObservableArray
            },
            error: function (err) {
                alert(err.status + " : " + err.statusText);
            }
        });
    },
};

self.editEmployees = function (student) {
    window.location.href =  '/EmployeeInfo/Edit/' + employee.EmpID;
};
self.deleteEmployees = function (student) {
    window.location.href = '/EmployeeInfo/Delete/' + employee.EmpID;
};

//Model
function Employees(data) {
    this.EmpID = ko.observable(data.EmpID);
    this.EmpName = ko.observable(data.EmpName);
    this.Salary = ko.observable(data.Salary);
    this.DeptName = ko.observable(data.DeptName);
    this.Designation = ko.observable(data.Designation);
   
}

fetchemployees.cshtml

@model IEnumerable<KOSetup.EmployeeInfo>

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Employee List</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table>
<thead>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.EmpName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Salary)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.DeptName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Designation)
        </th>
        <th></th>
    </tr>
    </thead>
<tbody data-bind="foreach: Employees">
        <tr>
            <td data-bind="text: EmpID"></td>
            <td data-bind="text: EmpName"></td>
            <td data-bind="text: Salary"></td>
            <td data-bind="text: DeptName"></td>
            <td data-bind="text: Designation"></td>
            
            <td>
                <a data-bind="click: editEmployees">Edit</a>
                <a data-bind="click: deleteEmployees">Delete</a>
            </td>
        </tr>
    </tbody>

</table>

@section Scripts {
    @Scripts.Render("~/Scripts/ViewModels/EmployeeListVM.js")
}

returned data

[{"EmpID":1,"EmpName":"yashica","Salary":12345678,"DeptName":"xyz","Designation":"xyz","EntityState":2,"EntityKey":{"EntitySetName":"EmployeeInfoes","EntityContainerName":"ExamplesEntities","EntityKeyValues":[{"Key":"EmpID","Value":1}],"IsTemporary":false}},{"EmpID":3,"EmpName":"pranali","Salary":12345678,"DeptName":"abc","Designation":"abc","EntityState":2,"EntityKey":{"EntitySetName":"EmployeeInfoes","EntityContainerName":"ExamplesEntities","EntityKeyValues":[{"Key":"EmpID","Value":3}],"IsTemporary":false}}]
Posted

1 solution

...
 success: function (data) { 
                for (var i = 0; i < data.length; i++) {
                    $("<tr> <td>
" + data[i].EmpID+ "</td>
<td>  " + data[i].EmpName+ "</td>
<td> " + data[i].Salary+ "</td>
<td>"+ data[i].DeptName+"</td> 
<td>"+data[i].Designation+"</td></tr>").appendTo("#tbEmployee");                   
                }
            },
 error: function (msg) { alert(msg); }
...
N.B.Give Table ID="tbEmployee"
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900