【发布时间】:2018-08-31 06:03:56
【问题描述】:
自从我开始学习 MVC 编程已经有好几天了,老实说,我仍在应对 MVC 的新环境。
在我的项目中,我开始使用这些代码创建一个数据表来显示我的数据库中的数据。
这是我在视图和控制器中的代码。这部分运行得很好。
<table id="table1" >
<thead>
<tr>
<th>id</th>
<th>title </th>
<th>
description
</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.id)
</td>
<td>
@Html.DisplayFor(modelItem => item.title)
</td>
<td>
@Html.DisplayFor(modelItem => item.description)
</td>
</tr>
}
</tbody>
</table>
}
var charts = (from p in db.pmTA_ProjectCategory
select new
{
id = p.id,
title = p.title,
description = p.description,
}).ToList()
.Select(x => new pmTA_ProjectCategory()
{
id = x.id,
title = x.title,
description = x.description,
});
return View(charts.ToList());
但我注意到我需要使用下拉菜单过滤我的数据,所以我再次将下拉菜单添加到我的视图中。
这是我在视图和控制器中的代码,用于显示下拉列表和下拉列表中的数据。
<div>
@Html.DropDownList("projectcat", ViewBag.proj as SelectList, "Select...", new {
@class = "form-control" })
</div>
var data1 = from p in db.pmTA_ProjectCategory
select new
{
id = p.id,
title = p.title,
description = p.description
};
SelectList list = new SelectList(data1, "id", "title");
ViewBag.proj = list;
当涉及到在下拉列表中显示数据时,它会再次顺利运行。
我的问题是,我需要使用下拉菜单自动过滤数据表的数据。我的意思是,当我选择下拉列表的值时,数据表必须显示数据对应于下拉列表中的选定值
我在 javascript 中创建了代码来使用下拉列表过滤数据表的数据。
这是代码:
<script>
$(document).ready(function () {
var table = $("#table1").DataTable();
$("#projectcat").change(function () {
table.search(this.value).draw();
});
});
</script>
但我的数据表中的数据没有响应且无法正常工作,当我在下拉列表中选择数据时,数据表无法过滤。
【问题讨论】:
-
它对我不起作用,但非常感谢您的帮助,我非常感谢它
标签: javascript c# jquery sql asp.net-mvc