如何将此 JavaScript 代码链接到 HTML 的锚标记:
<script type="text/javascript">
$(document).on('click', 'a', function () {
$.ajax({
type: 'POST',
url: '@Url.Action("/brandsOfACategory")',
contentType: 'application/json; charset:utf-8',
data: JSON.stringify({ id: this.id })
})
});
锚标签:
<a id="@c.Key" href ="???" onclick="???">@c.Key</a>
brandsOfACategory动作方法:
[HttpPost]
public ActionResult brandsOfACategory(string id)
{
var result = db.Items.Where(x => x.Category.Name.Equals(id)).Select(x => x.BrandID).ToList();
var ListOfBrands = db.Brands.Where(t => result.Contains(t.BrandID)).ToList();
return View(ListOfBrands);
}
brandsOfACategory.cshtml是:
@model IEnumerable<OnlineStore.Models.Brand>
@{
Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Brands in a Category</title>
</head>
<body>
@foreach (var i in Model)
{
@i.Name.ToString();
}
</body>
</html>
梦里花落0921
繁花如伊
相关分类