【发布时间】:2021-09-07 09:46:44
【问题描述】:
我希望无论单击哪个按钮,我都想调用相同的函数。 现在我在 View 上有六张卡,但首先只工作一张。其他工作
我的看法
<h2>Our workouts</h2>
<div class="container">
<div class="row">
@foreach (var item in Model)
{
<div class="col-md-4">
<div class="card" style="width: 18rem;">
<img src="~/Content/not-found-image.jpg" class="card-img-top" alt="...">
<div class="card-body">
<h5 class="card-title">@item.Name</h5>
<p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
<div id="textButton">
@*<a href="~/TrainingType/ListCoach" id="btnSave1" class="btn btn-primary">Go to anywhere</a>*@
<a id="btnNewSave" class="btn btn-primary">Go to anywhere</a>
<input type="text" id="getValue" value="@item.Id" hidden />
</div>
</div>
</div>
</div>
}
</div>
</div>
我的控制器:
[HttpGet]
public ActionResult ListCoach(int? Id)
{
var ChoachList = _TraningService.Coach(Id);
return View(ChoachList);
}
脚本 我从查看使用助手部分调用我的脚本
@section scripts {
<script>
$(document).ready(function () {
$("#btnNewSave").click(function () {
var data = $("#getValue").val();
$.ajax({
type: "GET",
url: '/TrainingType/ListCoach',
data: { id: data },
success: function (data) {
console.log(data);
window.location.href = '/TrainingType/ListCoach';
return data;
},
error: function () {
$("#loader").fadeOut("slow");
console.log("error1");
}
});
});
});
</script>
}
【问题讨论】:
-
给你的按钮一个 css 类,例如
some-class然后将您的 jQuery 代码更改为$('.some-class').click(function () { ... }) -
你有类型
<a>和type="button在这种情况下你必须使它们相似的类型或通过 Id 调用不同的函数然后在内部调用相同的函数。 -
请尝试更新后的答案。它会相应地解决您的问题。
标签: c# asp.net-mvc asp.net-core