【发布时间】:2021-11-04 21:09:24
【问题描述】:
我做错了什么? 我的方法被调用了两次。 我第一次得到 Id,这是正确的行为 我第二次得到 Null 我不明白什么是正确的解决方法?
我的看法
<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 class="btn btn-primary" data-index="@item.Id" name="link">Go to anywhere</a>
</div>
</div>
</div>
</div>
}
</div>
</div>
我的控制者
[HttpGet]
public ActionResult ListCoach(int id)
{
if (id != null)
{
var ChoachList = _TraningService.Coach(id);
return View(ChoachList);
}
return HttpNotFound();
}
脚本 我在我的视图上使用脚本使用帮助器@Section {}
@section scripts {
@*<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.2.1.min.js"></script>*@
<script>
function PassToContoller(data) {
//alert(data);
$.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");
}
});
}
$(document).ready(function () {
$("a[name=link]").on("click", function () {
var valueOfClickedTag = $(this).data("index");
alert("Clicked: " + valueOfClickedTag);
var callFunc = PassToContoller(valueOfClickedTag)
});
$(":button[id=GoToAnywhere3]").on("click", function () {
var valueOfClickedBtn = $(this).data("index");
var callFunc = PassToContoller(valueOfClickedBtn)
});
});
</script>
}
如果我使用该方法: 我无法进入 View ListCoach 我可以返回 Json on My View 吗?
[HttpGet]
public JsonResult ListCoach(int id)
{
var ChoachList = _TraningService.Coach(id);
return Json(ChoachList,JsonRequestBehavior.AllowGet);
}
【问题讨论】:
-
如果您想使用您的 ajax 请求重定向到同一个视图页面,您可以使用下面的语句来完成,就像在您的 ajax 中一样,您正在调用带有参数的 ListCoach 方法,并且成功调用了相同的没有参数的方法导致 id 参数为空。 var geturl='/TrainingType/ListCoach?id='+ 数据; window.location.href=geturl;
标签: asp.net-mvc asp.net-core asp.net-mvc-4