我的觀點是 C#:
@{
var itemList = (List<Item>)ViewData["itemsList"];
}
<div class="row" style="margin-top: 10px;">
<div class="col-md-6">
@if (itemList != null)
{
var id = 0;
<table class="table table-striped">
<thead>
<tr>
<th>#</th>
<th></th>
<th>Id</th>
<th>Type</th>
</tr>
</thead>
<tbody>
@foreach (var result in itemsList)
{
<tr>
<td>@( id)</td>
<td><input type="checkbox" value="true" @(result.Checked ? "checked" : "")></td>
<td>@result.Id</td>
<td>@result.Type</td>
</tr>
}
</tbody>
</table>
}
<div class="row justify-content-end" style="margin-top: 20px;">
<div class="col-md-2">
<form asp-controller="Control" asp-action="Remove" method="post">
<input type="hidden" name="tableName" value="table"/>
<input type="hidden" name="items" value="@itemList"/>
<div style="margin-left: -10px;" class="col-md-2">
<button class="btn btn-danger" title="Remove" type="submit">Remove</button>
</div>
</form>
</div>
</div>
</div>
</div>
我想從表中洗掉專案,用戶選中該復選框。我的想法是使用串列(result.Checked屬性)更新每個選中的專案,然后將陣列發送到 Remove 方法:
[HttpPost]
public async Task<IActionResult> Remove(string tableName, List<ChangeQueueItem> items)
{
try
{
var toDelete = items.Where(x => x.Checked == true);
await _repository.RemoveFromQueue(toDelete, tableName);
}
catch (Exception e)
{
TempData["error"] = e.Message;
}
return RedirectToAction("Index");
}
我正在嘗試像這樣發送該串列:
<input type="hidden" name="items" value="@itemList"/>
但是該值為空。我該怎么做?
更新:資料在此處加載:
[HttpGet]
public async Task<IActionResult> Index()
{
var items = await _repository.GetAll();
ViewData["itemsList"] = items;
ViewData["error"] = TempData["error"];
return View("Index");
}
uj5u.com熱心網友回復:
首先,您使用 設定值ViewData["itemsList"] = items;,但通過 獲取var itemList = (List<Item>)ViewData["itemList"];。
將鍵值更改為一致:例如在視圖中替換itemList為itemsList。
其次,要將串列從視圖傳遞給控制器??操作方法,將索引應用于專案(僅顯示<tbody>內容):
<tbody>
@using (Html.BeginForm("Remove", "Control"))
{
@Html.Hidden("tableName", "table")
@for (int i = 0; i < itemsList.Count; i )
{
@Html.Hidden("items[" i "].Id", itemsList[i].Id)
@Html.Hidden("items[" i "].Type", itemsList[i].Type)
<tr>
<td>@( id)</td>
<td>@Html.CheckBox("items[" i "].Checked", itemsList[i].Checked)</td>
<td>@itemsList[i].Id</td>
<td>@itemsList[i].Type</td>
</tr>
}
<tr><td><button class="btn btn-danger" title="Remove" type="submit">Remove</button></td></tr>
}
</tbody>
或者沒有助手也一樣(只顯示<tbody>內容):
<tbody>
<form asp-controller="Control" asp-action="Remove" method="post">
<input type="hidden" name="tableName" value="table" />
@for (int i = 0; i < itemsList.Count; i )
{
<input type="hidden" name="@("items[" i "].Id")" value="@itemsList[i].Id" />
<input type="hidden" name="@("items[" i "].Type")" value="@itemsList[i].Type" />
<tr>
<td>@( id)</td>
<td><input name="@("items[" i "].Checked")" type="checkbox" value="true" @(itemsList[i].Checked ? "checked" : " ") /></td>
<td>@itemsList[i].Id</td>
<td>@itemsList[i].Type</td>
</tr>
}
<tr><td><button class="btn btn-danger" title="Remove" type="submit">Remove</button></td></tr>
</form>
</tbody>
uj5u.com熱心網友回復:
您似乎沒有設定TempData["itemList"]; 我在代碼中看不到它。
您應該使用以下內容進行設定:
TempData["itemList"] = toDelete;
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/384702.html
標籤:C# asp.net-mvc asp.net核心 剃刀 剃刀页面
