我有一個 ASP.NET CORE mvc 專案,我在其中呼叫 API 并在表中顯示資料,其中包括玩家的原始資料和兩列用于更新和洗掉。問題是當我實作洗掉功能的引導模式時,它顯示成功的結果TempData,但未能完成動作并且玩家仍在桌子上!這是控制器中的洗掉操作
[HttpPost]
public async Task<IActionResult> DeletePlayer(int id)
{
var request = new HttpRequestMessage(HttpMethod.Delete, "http://localhost:42045/api/player/" id);
var client = _clientFactory.CreateClient();
HttpResponseMessage response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
if (response.StatusCode == System.Net.HttpStatusCode.NoContent)
{
TempData["success"] = "Player Deleted Successfully!";
return RedirectToAction("GetAllPlayers");
}
else
{
return BadRequest();
}
}
這是視圖
@model IEnumerable<Player>
@{
ViewData["title"] = "All Players";
}
<style>
.container {
max-width: 95vw;
}
</style>
<a asp-action="GetPlayer" class="btn btn-primary">Get Player</a>
<a asp-action="AddPlayer" class="btn btn-primary">Add Player</a>
<h2 class="h3 my-5 text-muted text-center fw-bold">A List of All Players</h2>
<div class="container" style="overflow:scroll">
<div class="row">
<div class="col">
<table class="table table-striped table-bordered text-center">
<thead>
<tr class="bg-secondary text-white">
<th>Id</th>
<th>Name</th>
<th>Position</th>
<th>Nationality</th>
<th>Update</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
@foreach (var player in Model)
{
<tr>
<td class="bg-dark text-white">@player.Id</td>
<td class="bg-warning">@player.Name</td>
<td class="bg-primary">@player.Position</td>
<td class="bg-info">@player.Nationality</td>
<td>
<a asp-action="UpdatePlayer" asp-route-id="@player.Id">
<i class="h3 bi bi-pencil-fill"></i>
</a>
</td>
<button type="button" data-bs-toggle="modal" data-bs-target="#exampleModal">
<i class="h3 bi bi-x-square-fill"></i>
</button>
<div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Delete Player</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
Are you sure?
</div>
<div class="modal-footer">
<form method="post" asp-controller="Home" asp-action="DeletePlayer">
<a class="btn btn-danger" data-bs-dismiss="modal">Close</a>
<input type="hidden" value="@player.Id" name="id" />
<button type="submit" class="btn btn-success">Yes</button>
</form>
</div>
</div>
</div>
</div>
<td>
</td>
</tr>
}
</tbody>
</table>
</div>
</div>
</div>
uj5u.com熱心網友回復:
<button type="button" data-bs-toggle="modal" data-bs-target="#exampleModal">
<i class="h3 bi bi-x-square-fill"></i>
</button>
<div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog">
<button>有一個類似的屬性data-bs-target,它定義了我們要打開的模式的 id。因此,data-bs-target 的值和模態的 id<div>必須相同。
您設定id="exampleModal"為第一個,因此 Modal 實作僅適用于第一個玩家而不是所有玩家。
嘗試改變上面 data-bs-target="#@player.Id"的<button> 和。然后將應用程式重定向到洗掉操作并洗掉播放器。id="@player.Id"<div>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/440803.html
標籤:C# api asp.net 核心 模型视图控制器 引导程序 5
