Note that there are some explanatory texts on larger screens.

plurals
  1. POMVC 4(Razor) KnockoutJS & DataTable
    primarykey
    data
    text
    <blockquote> <p>I am trying to show a DataTable in HTML table through knockout binding...I dont know where i am missing or wrong:</p> <p>My Controller:</p> </blockquote> <pre><code> public JsonResult GetEmployees() { BAL.Employee dbProvider = new BAL.Employee(); DataTable dataTable = dbProvider.ShowEmployeeDetails(); List&lt;Model.Employee&gt; objExerciseList = new List&lt;Model.Employee&gt;(); foreach (DataRow dataRow in dataTable.Rows) { Model.Employee objExercise = new Model.Employee(); objExercise.EmployeeCode = dataRow["EmpCode"].ToString(); objExercise.EmployeeName = dataRow["EmpName"].ToString(); objExerciseList.Add(objExercise); } return Json(objExerciseList, JsonRequestBehavior.AllowGet); } </code></pre> <blockquote> <p>My Model:</p> </blockquote> <pre><code>public class Employee { private string employeeCode; private string employeeName; public int ID { get; set; } [Required(ErrorMessage="Employee Code is Required")] public string EmployeeCode { get { return employeeCode; } set { employeeCode = value; } } [Required(ErrorMessage = "Employee Name is Required")] public string EmployeeName { get { return employeeName; } set { employeeName = value; } } } </code></pre> <blockquote> <p>This is my ViewModel Code:</p> </blockquote> <pre><code>@{ ViewBag.Title = "Exercise9"; Layout = "../Shared/Master.cshtml"; } &lt;html&gt; &lt;head&gt; &lt;title&gt;KO&lt;/title&gt; &lt;script src="../../Scripts/jquery-1.6.2.js" type="text/javascript"&gt;&lt;/script&gt; &lt;script src="../../Scripts/knockout-2.2.1.js" type="text/javascript"&gt;&lt;/script&gt; &lt;script src="../../Scripts/knockout.mapping-latest.js" type="text/javascript"&gt;&lt;/script&gt; &lt;script src="../../Scripts/json2.js" type="text/javascript"&gt;&lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;form action="" method="get"&gt; &lt;div style="width: 990px; background-color: White; height: 710px;"&gt; &lt;table id="tbllist" align="center" style="border:5px #fff solid;"&gt; &lt;tr&gt; &lt;td colspan="6"&gt; &lt;h2&gt; Employee List&lt;/h2&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td colspan="6" style="padding: 0px;"&gt; &lt;div id="title_p"&gt; Listing&lt;/div&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;th align="left"&gt; Employee Code &lt;/th&gt; &lt;th align="left"&gt; Employee Name &lt;/th&gt; &lt;/tr&gt; &lt;tbody data-bind="foreach: Employees"&gt; &lt;tr style="border-bottom: 1px solid #000000;"&gt; &lt;td&gt; &lt;span data-bind="text: EmployeeCode"&gt;&lt;/span&gt; &lt;/td&gt; &lt;td&gt; &lt;span data-bind="text: EmployeeName"&gt;&lt;/span&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;/table&gt; &lt;/div&gt; &lt;/form&gt; &lt;script type="text/javascript"&gt; var EmpViewModel = function () { //Make the self as 'this' reference var self = this; //Declare observable which will be bind with UI self.EmployeeCode= ko.observable("0"); self.EmployeeName= ko.observable(""); //The Object which stored data entered in the observables var EmpData = { EmpCode:self.EmployeeCode, EmpName: self.EmployeeName }; //Declare an ObservableArray for Storing the JSON Response self.Employees = ko.observableArray([]); GetEmployees(); //Call the Function which gets all records using ajax call //Function to Read All Employees function GetEmployees() { //Ajax Call Get All Employee Records $.ajax({ type: "GET", url: "/Exercise/GetEmployees/", contentType: "application/json; charset=utf-8", dataType: "json", success: function (data) { self.Employees(data); //Put the response in ObservableArray }, error: function (error) { alert(error.status + "&lt;--and--&gt; " + error.statusText); } }); //Ends Here } }; ko.applyBindings(new EmpViewModel()); &lt;/script&gt; </code></pre> <p> </p> <blockquote> <p>Please help me...THANKS IN ADVANCE....!!</p> </blockquote>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload