繁体   English   中英

选定的复选框未显示在 asp.net core mvc 3.1 的 HttpPost 操作方法中

[英]Selected Checkboxes are not showing up in the HttpPost action method in asp.net core mvc 3.1

我正在尝试在[HttpPost]操作方法中打印所有选中的复选框。 但是,我总是被计为 0。有人可以建议我哪里做错了吗?

下面是我的Category模型类。

public class Category
{
    public int CategoryId { get; set; }
    public string CategoryName { get; set; }
    public bool CheckboxAnswer { get; set; }
}

由于这是示例程序,我使用了如下所示的CategoryData类来绑定复选框列表控件。

public class CategoryData
{
    public CategoryData()
    {
    }

    public List<Category> Categories
    {
        get
        {
            var categories = new List<Category>
            {
                new Category { CategoryId = 1, CategoryName = "Beverages", CheckboxAnswer = true },
                new Category { CategoryId = 2, CategoryName = "Condiments", CheckboxAnswer = false },
                new Category { CategoryId = 3, CategoryName = "Confections", CheckboxAnswer = true },
                new Category { CategoryId = 4, CategoryName = "Dairy Products", CheckboxAnswer = false }
            };
            return categories;
        }
    }
}

下面是视图: GetSelectedCheckBoxes.cshtml

@model IEnumerable<Practice.MVC.Models.Category>
@{
    ViewBag.Title = "Get Selected CheckBoxes";
}
<h1>@ViewBag.Title</h1>
<form asp-controller="Test" asp-action="GetSelectedCheckBoxes" method="post">
    @foreach (var category in Model)
    {
        <input type="checkbox" asp-for="@category.CheckboxAnswer" />
        <label asp-for="@category.CategoryId">@category.CategoryName</label>
        <input type="hidden" asp-for="@category.CategoryId" />
        <input type="hidden" asp-for="@category.CategoryName" />
    }
    <input type="submit" id="Submit" name="Submit" value="Submit" />
</form>

下面是我的Controller操作方法的[HttpGet]版本。

    public IActionResult GetSelectedCheckBoxes()
    {
        var categories = new CategoryData().Categories;
        return View(categories);
    }

下面是我的Controller操作方法的[HttpPost]版本。

    [HttpPost]
    public IActionResult GetSelectedCheckBoxes(List<Category> categories)
    {
        string selectedCheckboxes = string.Empty;
        string notSelectedCheckboxes = string.Empty;
        foreach (var category in categories)
        {
            if (category.CheckboxAnswer)
                selectedCheckboxes += category.CategoryName + ", ";
            if (!category.CheckboxAnswer)
                notSelectedCheckboxes += category.CategoryName + ", ";
        }
        return Content(selectedCheckboxes + " ------ " + notSelectedCheckboxes);
    }

正如您在下面的屏幕截图中看到的,我没有将categories作为参数在此处输入图像描述

尝试使用以下代码:

@model IList<Practice.MVC.Models.Category>
@{
    ViewBag.Title = "Get Selected CheckBoxes";
}
<h1>@ViewBag.Title</h1>
<form asp-controller="Test" asp-action="GetSelectedCheckBoxes" method="post">
     
        @*@foreach (var category in Model)*@
        @for(int i=0; i < Model.Count; i++) 
        {
            @Html.CheckBox("categories[" + i + "].CheckboxAnswer", Model[i].CheckboxAnswer)

            <label asp-for="@Model[i].CategoryId">@Model[i].CategoryName</label>

            @Html.Hidden("categories[" + i + "].CategoryId", Model[i].CategoryId)
            @Html.Hidden("categories[" + i + "].CategoryName", Model[i].CategoryName)
        }
   
    <input type="submit" id="Submit" name="Submit" value="Submit" />
</form>

要使 MVC 绑定正常工作,必须对集合项使用索引。

在此处输入图像描述

因此,为了使代码在IEnumerable上方的片段中更短,被IList替换,并且使用for而不是foreach

此外, <input>标签助手为asp-for属性中指定的表达式名称生成idname HTML 属性。 但在这种情况下,索引不会包含在 'id' 和name中。 请参阅输入标签助手

这就是为什么使用HTML Helper 替代 Input Tag Helper的原因。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM