ASP.NET 核心 MVC 将 razor 视图中的表单中的选中项返回到控制器
ASP.NET Core MVC returning checked items from a form in a razor view to controller
我正在使用一个显示已除名图书列表的视图(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>
}
所以我的问题是:如何在我的控制器操作中处理这些数据。当我点击提交时,return IEnumerable
of Books with 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
为true.YOu的图书列表可以添加隐藏输入绑定BookName,Author,Publisher
的数据。然后更改name属性在表单 submitting.Here 之前选中复选框时输入正确的格式是一个演示:
操作:
[HttpGet]
public IActionResult DelistedForm()
{
return View(new List<Book> { new Book { Author="a1", BookName="b1", IsActive=false, Publisher="p1"},
new Book { Author = "a2", BookName = "b2", IsActive = false, Publisher = "p2" },
new Book { Author = "a3", BookName = "b3", IsActive = false, Publisher = "p3" } });
}
[HttpPost]
[Route("/Book/DelistedForm")]
public ActionResult DelistedForm(IEnumerable<Book> fromDelist)
{
foreach (var item in fromDelist)
{
if (item.IsActive == true) ;
// enlist code here
}
return View("~/Views/Home/Index.cshtml");
}
查看:
@if (Model != null)
{
@using (Html.BeginForm("DelistedForm", "Book", FormMethod.Post,new { @id="myForm"}))
{
<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)
@Html.HiddenFor(modelItem => item.BookName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Author)
@Html.HiddenFor(modelItem => item.Author)
</td>
<td>
@Html.DisplayFor(modelItem => item.Publisher)
@Html.HiddenFor(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>
}
js:
<script>
$("#myForm").submit(function () {
var count = 0;
$("tbody tr").each(function () {
if ($(this).find("#item_IsActive").prop("checked")) {
$(this).find("#item_BookName").attr("name", "fromDelist[" + count + "].BookName");
$(this).find("#item_Author").attr("name", "fromDelist[" + count + "].Author");
$(this).find("#item_Publisher").attr("name", "fromDelist[" + count + "].Publisher");
$(this).find("#item_IsActive").attr("name", "fromDelist[" + count + "].IsActive");
count++;
}
})
})
</script>
结果:
我正在使用一个显示已除名图书列表的视图(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>
}
所以我的问题是:如何在我的控制器操作中处理这些数据。当我点击提交时,return IEnumerable
of Books with 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
为true.YOu的图书列表可以添加隐藏输入绑定BookName,Author,Publisher
的数据。然后更改name属性在表单 submitting.Here 之前选中复选框时输入正确的格式是一个演示:
操作:
[HttpGet]
public IActionResult DelistedForm()
{
return View(new List<Book> { new Book { Author="a1", BookName="b1", IsActive=false, Publisher="p1"},
new Book { Author = "a2", BookName = "b2", IsActive = false, Publisher = "p2" },
new Book { Author = "a3", BookName = "b3", IsActive = false, Publisher = "p3" } });
}
[HttpPost]
[Route("/Book/DelistedForm")]
public ActionResult DelistedForm(IEnumerable<Book> fromDelist)
{
foreach (var item in fromDelist)
{
if (item.IsActive == true) ;
// enlist code here
}
return View("~/Views/Home/Index.cshtml");
}
查看:
@if (Model != null)
{
@using (Html.BeginForm("DelistedForm", "Book", FormMethod.Post,new { @id="myForm"}))
{
<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)
@Html.HiddenFor(modelItem => item.BookName)
</td>
<td>
@Html.DisplayFor(modelItem => item.Author)
@Html.HiddenFor(modelItem => item.Author)
</td>
<td>
@Html.DisplayFor(modelItem => item.Publisher)
@Html.HiddenFor(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>
}
js:
<script>
$("#myForm").submit(function () {
var count = 0;
$("tbody tr").each(function () {
if ($(this).find("#item_IsActive").prop("checked")) {
$(this).find("#item_BookName").attr("name", "fromDelist[" + count + "].BookName");
$(this).find("#item_Author").attr("name", "fromDelist[" + count + "].Author");
$(this).find("#item_Publisher").attr("name", "fromDelist[" + count + "].Publisher");
$(this).find("#item_IsActive").attr("name", "fromDelist[" + count + "].IsActive");
count++;
}
})
})
</script>
结果: