ASP.NET核心MVC从razor视图中的窗体将选中的项返回给控制器
我正在使用一个视图,该视图显示一个退市图书列表(isActive=false)。在这个视图中,我为每本书添加了一列复选框。提交表单时,我希望将所有选中的书籍设置为活动状态(isActive=true) 这是我正在处理的观点:ASP.NET核心MVC从razor视图中的窗体将选中的项返回给控制器,razor,asp.net-core-mvc,Razor,Asp.net Core Mvc,我正在使用一个视图,该视图显示一个退市图书列表(isActive=false)。在这个视图中,我为每本书添加了一列复选框。提交表单时,我希望将所有选中的书籍设置为活动状态(isActive=true) 这是我正在处理的观点: @model IEnumerable<MvcBooksList.Models.Book> @{ ViewData["Title"] = "Delisted Books"; } @if (Model != nu
@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>
}
如果您只想传递一个
IsActive
为true的图书列表,则可以添加隐藏输入以绑定BookName、Author、Publisher
的数据。然后在提交表单之前选中复选框时,将输入的name属性更改为正确的格式。以下是一个演示:
行动:
[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");
}
[HttpGet]
公共IActionResult DelistedForm()
{
返回视图(新列表{new Book{Author=“a1”,BookName=“b1”,IsActive=false,Publisher=“p1”},
新书{Author=“a2”,BookName=“b2”,IsActive=false,Publisher=“p2”},
新书{Author=“a3”,BookName=“b3”,IsActive=false,Publisher=“p3”});
}
[HttpPost]
[路线(“/Book/DelistedForm”)]
公共行动结果DelistedForm(从Delist中可数)
{
foreach(fromDelist中的var项目)
{
如果(item.IsActive==true);
//在这里登记代码
}
返回视图(“~/Views/Home/Index.cshtml”);
}
视图:
@if(Model!=null)
{
@使用(Html.BeginForm(“DelistedForm”、“Book”、FormMethod.Post、new{@id=“myForm”}))
{
@DisplayNameFor(model=>model.BookName)
@DisplayNameFor(model=>model.Author)
@DisplayNameFor(model=>model.Publisher)
征集
@foreach(模型中的var项目)
{
@DisplayFor(modelItem=>item.BookName)
@Html.HiddenFor(modelItem=>item.BookName)
@DisplayFor(modeleItem=>item.Author)
@Html.HiddenFor(modeleItem=>item.Author)
@DisplayFor(modelItem=>item.Publisher)
@Html.HiddenFor(modeleItem=>item.Publisher)
@CheckBoxFor(modelItem=>item.IsActive)
}
}
}
其他的
{
目前没有退市的书籍
}
js:
$(“#myForm”).submit(函数(){
var计数=0;
$(“tbody tr”)。每个(函数(){
if($(this).find(“#item_IsActive”).prop(“选中”)){
$(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”);
计数++;
}
})
})
结果:
那么,当您单击“提交”时,您想传递一个
IsActive
为真的图书列表?或者您想获取一个完整的图书列表,然后获取一个IsActive
为真的图书列表?您想要哪一本?@yiyiyi最初所有图书都将IsActive设置为false。我想传递检查过的图书(勾选复选框),如果可能的话..我已经发布了我的答案.太棒了.非常感谢你的回答。。
[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>
}
<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>