Model view controller 在MVC视图中显示数据表

Model view controller 在MVC视图中显示数据表,model-view-controller,knockout.js,datatable,Model View Controller,Knockout.js,Datatable,我想在KnockoutJS的帮助下,从DataTable绑定HTML表中的员工详细信息。这是我的模型: 公共类员工 { 私有字符串employeeCode; 私有字符串employeeName; 公共int ID{get;set;} [必需(ErrorMessage=“需要员工代码”)] 公共字符串EmployeeCode { 得到 { 返回员工代码; } 设置 { employeeCode=值; } } [必需(ErrorMessage=“需要员工姓名”)] 公共字符串EmployeeName

我想在KnockoutJS的帮助下,从DataTable绑定HTML表中的员工详细信息。这是我的模型:

公共类员工
{
私有字符串employeeCode;
私有字符串employeeName;
公共int ID{get;set;}
[必需(ErrorMessage=“需要员工代码”)]
公共字符串EmployeeCode
{
得到
{
返回员工代码;
}
设置
{
employeeCode=值;
}
}
[必需(ErrorMessage=“需要员工姓名”)]
公共字符串EmployeeName
{
得到
{
返回员工姓名;
}
设置
{
employeeName=值;
}
}
}
这是我使用DataTable的控制器代码。我正在将
列表
传递给我的视图:

publicjsonresult-Get(int-customerID)
{
BAL.Employee dbProvider=新的BAL.Employee();
DataTable DataTable=dbProvider.ShowEmployeeDetails();
List objExerciseList=新列表();
foreach(dataTable.Rows中的DataRow-DataRow)
{
Model.Employee objExercise=new Model.Employee();
objExercise.ID=Convert.ToInt32(dataTable.Rows[0][“ID”].ToString());
objExercise.EmployeeCode=dataTable.Rows[0][“EmpCode”].ToString();
objExercise.EmployeeName=dataTable.Rows[0][“EmpName”].ToString();
objExercise.ContactNumber=dataTable.Rows[0][“ContactNumber”].ToString();
objExercise.MaritalStatus=Convert.ToBoolean(dataTable.Rows[0][“Is_MaritalStatus”].ToString());
objExercise.EmailID=dataTable.Rows[0][“EmailID”].ToString();
添加(objExercise);
}
返回Json(objExerciseList,JsonRequestBehavior.AllowGet);
}
最后是我的视图和视图模型页面&代码:

@model IEnumerable
@{
ViewBag.Title=“exercise7”;
Layout=“../Shared/Master.cshtml”;
}
击倒对手
员工名单
表册
员工代码
员工姓名
联系电话
婚姻状况
电子邮件ID
@LabelFor(model=>model.EmployeeCode,新的{data\u bind=“text:EmpCode”})
@LabelFor(model=>model.EmployeeName,新的{data\u bind=“text:EmpName”})
@LabelFor(model=>model.ContactNumber,新的{data\u bind=“text:ContactNumber”})
@CheckBoxFor(model=>model.MaritalStatus,new{data_bind=“checked:MaritalStatus”})
@LabelFor(model=>model.EmailID,新的{data_bind=“text:EmailID”})
//初始化名称空间
var KnockoutDemoNamespace={};
//视图模型声明
KnockoutDemoNamespace.initViewModel=函数(objExercise){
var customerViewModel={
EmpCode:ko.可观测(objExercise.EmployeeCode),
EmpName:ko.observable(objExercise.EmployeeName),
ContactNumber:ko.可观察(objExercise.ContactNumber),
MaritalStatus:ko.可观察(objExercise.MaritalStatus),
EmailID:ko.observable(objExercise.EmailID)
};
返回customerViewModel;
}
//约束客户
KnockoutDemoNamespace.bindData=函数(objExercise){
//创建视图模型
var viewModel=KnockoutDemoNamespace.initViewModel(objExercise);
应用绑定(视图模型);
}
KnockoutDemoNamespace.getCustomer=函数(customerID){
$.ajax({
url:“/Exercise/Get/”,
键入:“post”,
数据:“{'customerID':'1'}”,
contentType:'应用程序/json',
成功:功能(结果){
KnockoutDemoNamespace.bindData(结果);
},
错误:函数(jqXHR、textStatus、errorshown){
var errorMessage='';
$('#message').html(jqXHR.responseText);
}
});
}
KnockoutDemoNamespace.addCustomer=函数(){
$.ajax({
url:“/Exercise/Add/”,
键入:“post”,
数据:ko.toJSON(这个),
contentType:'应用程序/json',
成功:功能(结果){
$('#message').html(结果);
}
});
}
$(文档).ready(函数(){
KnockoutDemoNamespace.getCustomer(1);
});

请帮帮我,我对KnockoutJS一无所知。如果我的问题有任何问题,请告诉我。

KnockoutJS设计用于AJAX和JSON。你的服务
        <tbody data-bind="foreach: model">
            <tr style="border-bottom: 1px solid #000000;">
                <td>
                    @Html.LabelFor(model => model.EmployeeCode, new { data_bind = "text: EmpCode" })
                </td>
                <td>
                    @Html.LabelFor(model => model.EmployeeName, new { data_bind = "text: EmpName" })
                </td>
                <td>
                    @Html.LabelFor(model => model.ContactNumber, new { data_bind = "text: ContactNumber" })
                </td>
                <td>
                    @Html.CheckBoxFor(model => model.MaritalStatus, new { data_bind = "checked: MaritalStatus" })
                </td>
                <td>
                    @Html.LabelFor(model => model.EmailID, new { data_bind = "text: EmailID" })
                </td>
            </tr>
        </tbody>