【发布时间】:2018-03-04 07:13:03
【问题描述】:
我是 ASP.NET 的新手,正在尝试构建购物应用程序。我正在使用 jquery.datatable 和 ajax 在表中显示商店中的所有商品:
@model IEnumerable<OnlineShoppingApp.Models.Category>
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<div class="col-md-2">
<ul id="categories">
@foreach(var category in Model)
{
<li data-category-id="@category.Id">@category.Name</li>
}
</ul>
</div>
<div class="col-md-10">
<table id="items" class=" table table-bordered table-hover">
<thead>
<tr>
<th>Name</th>
<th>Description</th>
<th>Price</th>
<th>Category</th>
</tr>
</thead>
<tbody></tbody>
</table>
</div>
@section Scripts {
<script>
$(document).ready(function () {
$("#items").DataTable(
{
ajax:
{
url: "/api/items",
dataSrc: ""
},
columns:
[
{
data: "Name",
},
{
data: "Description",
},
{
data: "Price",
},
{
data: "Category.Name",
}
]
}
)
$("li").click(function () {
var element = $(this);
});
});
</script>
}
在 CategoryController 中:
public ActionResult Index()
{
var categories = context.Categories.ToList();
return View(categories);
}
在左侧我列出了所有类别,当单击一个时,我想过滤此数据表以仅显示与所选类别匹配的项目。我不想让页面重新加载,而且我不知道从哪里开始。
【问题讨论】:
标签: jquery asp.net-mvc-5 datatables asp.net-ajax