asp.net-mvc - 方法被调用两次
问题描述
我做错了什么?我的方法被调用了两次。我第一次得到 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();
}
脚本 我在 My View上使用脚本使用助手 @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 我可以在 My View 上返回 Json 吗?
[HttpGet]
public JsonResult ListCoach(int id)
{
var ChoachList = _TraningService.Coach(id);
return Json(ChoachList,JsonRequestBehavior.AllowGet);
}
解决方案
在你的函数PassToContoller
中,在 ajax 中success
,你使用window.location.href = '/TrainingType/ListCoach';
,所以在你调用函数之后,它将有两个请求,一个使用 ajax,一个使用window.location.href
.
如果要在ajax中获取jsondata,需要dataType: "json",
在ajax中使用。这是一个传递 json 数据的演示:
行动:
[HttpGet]
public JsonResult GetJson(int id)
{
return new JsonResult(new TestModel { Id=id,Name="Test"});
}
看法:
<button onclick="test()">
test
</button>
@section scripts
{
<script>
function test() {
$.ajax({
type: "GET",
url: 'GetJson',
data: { id: 1 },
dataType: "json",
success: function (data) {
console.log(data);
},
error: function () {
$("#loader").fadeOut("slow");
console.log("error1");
}
});
}
</script>
}
更新:
您可以尝试使用表单将 Id 传递给操作(不使用<a></a>
):
<form asp-action="GetJson" asp-route-id="@item.Id">
<input type="submit" value="Go to anywhere" />
</form>
行动:
[HttpGet]
public IActionResult GetJson(int id)
{
return View("Index",new List<TestModel> { new TestModel { Id = id, Name = "Test" } });
}
推荐阅读
- perl - 在 Moose 中,如何为另一个 rw 属性创建 rw 代理属性
- selenium - Robot Framework 无法找到正确的链接地址
- powershell - 检索 Powershell 脚本的 REST API 响应标头值
- css - 如果未选择任何选项,带有 ModelChoiceField(required=True) 的表单不会引发 ValidationError
- c++ - 在 C++ 类用法中尝试 catch 块
- python - 谷歌云中的 Python 3.8 函数 JSONDecodeError 在 json.loads 和 json.dumps
- vba - VBA 运行多次
- c++ - C++20 范围自定义视图:尝试通过管道传输到另一个视图时出错
- loops - 赛普拉斯没有找到任何测试 - 对站点地图中的网址进行测试
- node.js - npm install 与 package.json 中的范围