-1

ノックアウトバインディングを介してHTMLテーブルにDataTableを表示しようとしています...どこが欠落しているか間違っているかわかりません:

私のコントローラー:

     public JsonResult GetEmployees()
     {
        BAL.Employee dbProvider = new BAL.Employee();

        DataTable dataTable = dbProvider.ShowEmployeeDetails();

        List<Model.Employee> objExerciseList = new List<Model.Employee>();

        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);
    }

私のモデル:

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;
    }
  }
}

これは私のViewModelコードです:

@{
ViewBag.Title = "Exercise9";
Layout = "../Shared/Master.cshtml";
}

<html>
<head>
<title>KO</title>
<script src="../../Scripts/jquery-1.6.2.js" type="text/javascript"></script>
<script src="../../Scripts/knockout-2.2.1.js" type="text/javascript"></script>
<script src="../../Scripts/knockout.mapping-latest.js" type="text/javascript"></script>
<script src="../../Scripts/json2.js" type="text/javascript"></script>
</head>
<body>
<form action="" method="get">
<div style="width: 990px; background-color: White; height: 710px;">
    <table id="tbllist" align="center" style="border:5px #fff solid;">
        <tr>
            <td colspan="6">
                <h2>
                    Employee List</h2>
            </td>
        </tr>
        <tr>
            <td colspan="6" style="padding: 0px;">
                <div id="title_p">
                    Listing</div>
            </td>
        </tr>
        <tr>
            <th align="left">
                Employee Code
            </th>
            <th align="left">
                Employee Name
            </th>
        </tr>
        <tbody data-bind="foreach: Employees">
            <tr style="border-bottom: 1px solid #000000;">
                <td>
                    <span data-bind="text: EmployeeCode"></span>
                </td>
                <td>
                    <span data-bind="text: EmployeeName"></span>
                </td>
            </tr>
        </tbody>
    </table>
</div>
</form>
<script type="text/javascript">
    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 + "<--and--> " + error.statusText);
                    }
               });
        //Ends Here
    }
};

ko.applyBindings(new EmpViewModel());
</script>

私を助けてください...事前に感謝します....!!

4

1 に答える 1

2

次のように、Jsonから監視可能なJavaScriptモデルに変換してみてください。

$.ajax({
                    type: "GET",
                    url: "/Exercise/GetEmployees/",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (data) {
                         ko.mapping.fromJS(data, {}, self.Employees);
                    },
                    error: function (error) {
                    alert(error.status + "<--and--> " + error.statusText);
                    }
               });
于 2013-01-29T07:53:15.190 に答える