【发布时间】:2018-08-23 20:15:37
【问题描述】:
我创建了一个带有 ActionResult Index 的控制器并创建了一个 Student 类列表:
public ActionResult Index()
{
var list = new List<Student>()
{
new Student{Id=1,RegNo="Bcs153048",Name="Ali",Age=21,},
new Student{Id=2,RegNo="Bcs153044",Name="Talha",Age=22,},
new Student{Id=3,RegNo="Bcs153064",Name="Luqman",Age=20,},
new Student{Id=4,RegNo="Bcs153054",Name="Saad",Age=19,},
new Student{Id=5,RegNo="Bcs153036",Name="Hashir",Age=20,},
};
//var jsonString = JsonConvert.SerializeObject(list);
//return View(list);
return Json(list , JsonRequestBehavior.AllowGet);
}
我想查看 JQuery 数据表中的学生列表,我做了这样的事情:
<table id="students" class="table table-bordered table-hover">
<thead>
<tr>
<th>Id</th>
<th>Registeration No</th>
<th>Name</th>
<th>Age</th>
</tr>
</thead>
<tbody></tbody>
然后在下面我写了脚本
@section scripts
{
<script>
$(document).ready( function () {
var dataTable = $("#students").DataTable({
ajax: {
url: "/student/index",
dataSrc: "",
},
columns: [
{
data: "Id"
},
{
data: "RegNo",
},
{
data: "Name"
},
{
data: "Age",
}
]
});
});
</script>
}
但是当我运行应用程序并导航到 /Student/index 时我得到了 Json 结果,我想在 Jquery 数据表中显示列表:
[{"Id":1,"Name":"Ali","Age":21,"RegNo":"Bcs153048"},{"Id":2,"Name":"Talha","年龄":22,"RegNo":"Bcs153044"},{"Id":3,"Name":"Luqman","Age":20,"RegNo":"Bcs153064"},{"Id":4 ,"姓名":"Saad","年龄":19,"RegNo":"Bcs153054"},{"Id":5,"Name":"Hashir","年龄":20,"RegNo":" Bcs153036"}]
我在 Bundle.config 中添加了如下库: Libraries in BundleConfig
【问题讨论】:
标签: asp.net asp.net-mvc jquery-plugins datatables