【发布时间】:2021-05-22 04:40:50
【问题描述】:
我正在使用一个显示已除名图书列表的视图(isActive = false)。在这个视图中,我为每本书添加了一列复选框。提交表单后,我想将所有选中的书籍设置为活动状态(isActive = true)。
这是我正在使用的视图:
@model IEnumerable<MvcBooksList.Models.Book>
@{
ViewData["Title"] = "Delisted Books";
}
@if (Model != null)
{
@using (Html.BeginForm("DelistedForm", "Book", FormMethod.Post))
{
<table class="table">
<thead>
<tr>
<th>
@Html.DisplayNameFor(model => model.BookName)
</th>
<th>
@Html.DisplayNameFor(model => model.Author)
</th>
<th>
@Html.DisplayNameFor(model => model.Publisher)
</th>
<th>
Enlist
</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(modelItem => item.BookName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Author)
</td>
<td>
@Html.DisplayFor(modelItem => item.Publisher)
</td>
<td>
@Html.CheckBoxFor(modelItem => item.IsActive)
</td>
</tr>
}
</tbody>
</table>
<input type="submit" value="Save" /> <input type = "button" value = "Cancel" onclick = "location.href='@Url.Action("Index","Home")'" />
}
}
else
{
<h3>No Books currently delisted</h3>
}
所以我的问题是:如何在我的控制器操作中处理这些数据。就像我点击提交时一样,表单是否会返回 IEnumerable of Books 并将 IsActive 设置为 true 以执行操作??
我正在尝试这样的事情:
[HttpPost]
public ActionResult DelistedForm(IEnumerable<Book> fromDelist)
{
foreach (var item in fromDelist)
{
if (item.IsActive == true) ;
// enlist code here
}
return View("~/Views/Home/Index.cshtml");
}
【问题讨论】:
-
所以你想传递一个
IsActive为真的书籍列表,当你点击提交时?或者你想得到一个完整的书籍列表,然后得到一个IsActive是的书籍列表真正的行动?你想要哪一个? -
@YiyiYou 最初所有书籍的 IsActive 为 false。如果可能的话,我想通过已选中的书籍(勾选复选框)..
-
我已经发布了我的答案。