我做錯了什么?我的方法被呼叫了兩次。我第一次得到 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 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);
}
uj5u.com熱心網友回復:
您的代碼似乎沒問題,理論上,您的操作不應呼叫兩次,但我想向您展示一些方法來解決問題:
- 當在選項卡網路中的 chrome 開發人員工具中對服務器進行呼叫時,您將看到一條記錄,其中有一個名為 Initiator 的列,顯示呼叫是由誰和在哪里創建的。
- 您可以在函式 PassToContoller 的第一個中使用關鍵字 DEBUG 并查看堆疊以了解誰呼叫了您的函式
關于您的最后一個問題,我的回答是否定的,您不能使用這種方式將 JSON 物件傳遞給您的視圖,但您可以通過 ViewBag 或 ViewData 在模型旁邊傳遞 JSON 物件
uj5u.com熱心網友回復:
在你的函式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" } });
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/422233.html
標籤:
