0
我很新的ASP.NET和嘗試建立購物應用程序。我使用jquery.datatable和Ajax顯示在錶店裏的所有項目:ASP.NET的jQuery數據過濾
@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);
}
在左邊我列出所有類別,當點擊一個我想過濾這個數據表只顯示匹配所選類別的項目。我不想讓頁面重新加載,我也不知道從哪裏開始。
查看https://datatables.net/reference/option/ajax – Shoe