Asp.net mvc MVC4(Razor)淘汰赛JS和DataTable
我试图通过敲除绑定在HTML表中显示DataTable…我不知道我在哪里丢失或错误: 我的控制器: 这是我的ViewModel代码: 请帮帮我…提前谢谢Asp.net mvc MVC4(Razor)淘汰赛JS和DataTable,asp.net-mvc,json,knockout.js,Asp.net Mvc,Json,Knockout.js,我试图通过敲除绑定在HTML表中显示DataTable…我不知道我在哪里丢失或错误: 我的控制器: 这是我的ViewModel代码: 请帮帮我…提前谢谢 尝试将Json转换为可观察的JavaScript模型,如下所示: $.ajax({ type: "GET", url: "/Exercise/GetEmployees/", contentType: "application/
尝试将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);
}
});
你们为什么不回答我的问题而降低我的观点这是帮助初学者的正确方法吗
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;
}
}
}
@{
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>
$.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);
}
});