【问题标题】:ASP.NET Core MVC returning checked items from a form in a razor view to controllerASP.NET Core MVC 将选中的项目从剃刀视图中的表单返回到控制器
【发布时间】: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。如果可能的话,我想通过已选中的书籍(勾选复选框)..
  • 我已经发布了我的答案。

标签: razor asp.net-core-mvc


【解决方案1】:

如果您只想传递IsActive 为真的书籍列表。您可以添加隐藏输入来绑定BookName,Author,Publisher 的数据。然后在选中复选框时将输入的名称属性更改为正确格式在表单提交之前。这是一个演示:

行动:

[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>

结果:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-19
    • 2011-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-17
    • 1970-01-01
    • 2021-01-22
    相关资源
    最近更新 更多