Asp.net core mvc ASP.NET Core 3.1 MVC:将所选复选框发送到控制器

Asp.net core mvc ASP.NET Core 3.1 MVC:将所选复选框发送到控制器,asp.net-core-mvc,Asp.net Core Mvc,我有以下带有复选框的列表视图: @model IEnumerable<PaketServisAracTakip.Models.Item> @{ ViewData["Title"] = "Yükleme Yap"; Layout = "~/Views/Shared/_Layout.cshtml"; } <h2 class="text-center text-success">

我有以下带有复选框的列表视图:

@model IEnumerable<PaketServisAracTakip.Models.Item>

@{
    ViewData["Title"] = "Yükleme Yap";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2 class="text-center text-success">@ViewBag.name İsimli Araca Yükleme Yap</h2>

<form asp-controller="Vehicle"
      asp-action="LoadItem" method="post">
    <br />
    <input type="submit" name="submit" value="Oluştur" class="btn btn-primary" />
</form>

<table class="table table-bordered table-striped table-hover">
    <thead>
        <tr>
            <th class="text-center">
                Yüklensin mi?
            </th>
            <th class="text-center">
                @Html.DisplayNameFor(model => model.Name)
            </th>
            <th class="text-center">
                @Html.DisplayNameFor(model => model.Price)
            </th>
            <th class="text-center">
                @Html.DisplayNameFor(model => model.Description)
            </th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model)
        {
            <tr>
                <td>
                    <div class="form-check">
                        <label class="form-check-label" for="@item.Id" asp-validation-for="@item.Id"></label>
                        <input class="form-check-input" name="ids" type="checkbox" value="@item.Id" id="@item.Id">
                    </div>
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Name)
                </td>
                <td>
                    ₺@Html.DisplayFor(modelItem => item.Price)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Description)
                </td>
            </tr>
        }
    </tbody>
</table>

所以,当点击按钮时,我想在我的控制器中获取选中的项目。我试过这个,但它是空的:

[HttpPost]
public ActionResult LoadItem(IEnumerable<Item> model)
{
    return RedirectToAction("Index");
}
[HttpPost]
公共操作结果加载项(IEnumerable模型)
{
返回操作(“索引”);
}
我也尝试了int数组和FormCollection,但没有成功。我想我需要一些标签助手,但不知道是哪个

单击按钮时,我希望在控制器中获取选中的项目。我 我试过这个,但它是空的

请检查查看页面中的代码,因为该表不在
元素中,当您单击
提交
按钮时,提交的表单不包含相关数据

此外,要使用模型绑定将模型数据提交给控制器,我们应该使用
@for
语句循环实体,并使用隐藏字段存储相关数据。请参考以下示例并更改代码:

型号:

[Table("Items")]
public class Item
{
    [Key]
    public int Id { get; set; }
    [Display(Name = "İsim")]
    [Required(ErrorMessage = "{0} alanı boş bırakılamaz.")]
    [MaxLength(50, ErrorMessage = "İsim 50 karakteri geçemez.")]
    public String Name { get; set; }
    [Display(Name = "Fiyat")]
    [Required(ErrorMessage = "{0} alanı boş bırakılamaz.")]
    [Range(0, Double.MaxValue, ErrorMessage = "Minimum 0 girmelisin.")]
    public int Price { get; set; }
    [Display(Name = "Açıklama")]
    public String Description { get; set; }

    public Boolean IsChecked { get; set; } //add a property to store whether the item is check or not.
}
查看页面:

@model List<netcore3_1sample.Models.Item>

@{
    ViewData["Title"] = "ItemIndex";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
 
<h2 class="text-center text-success">@ViewBag.name İsimli Araca Yükleme Yap</h2>

<form asp-controller="Home"
      asp-action="LoadItem" method="post">
    <br /> 
    <input type="submit" name="submit" value="Oluştur" class="btn btn-primary" />
    <br />
    <table class="table table-bordered table-striped table-hover">
        <thead>
            <tr>
                <th class="text-center">
                    Yüklensin mi?
                </th>
                <th class="text-center">
                    Name
                </th>
                <th class="text-center">
                    Price
                </th>
                <th class="text-center">
                    Description
                </th>
            </tr>
        </thead>
        <tbody> 
            @for( var i = 0; i < Model.Count;i++)
            {
                <tr>
                    <td>
                        <div class="form-check">
                            <label class="form-check-label" for="@Model[i].Id" asp-validation-for="@Model[i].Id"></label>
                            <input class="form-check-input" name="ids" type="checkbox" value="@Model[i].Id" id="@Model[i].Id">
                            @*<input type="checkbox" asp-for="@Model[i].IsChecked" />*@
                            <input type="hidden" asp-for="@Model[i].Id" />
                        </div>
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => Model[i].Name)
                        <input type="hidden" asp-for="@Model[i].Name" />
                    </td>
                    <td>
                        ₺@Html.DisplayFor(modelItem => Model[i].Price)
                        <input type="hidden" asp-for="@Model[i].Price" />
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => Model[i].Description)
                        <input type="hidden" asp-for="@Model[i].Description" />
                    </td>
                </tr>
            }
        </tbody>
    </table>
</form>


通过使用此方法,在控制器中,您可以根据IsChecked属性筛选所选项目

结果如下:


谢谢,我解决了这个问题,但在这种情况下for和foreach有什么区别?我已经检查了
for
foreach
语句,似乎在使用
foreach
语句时,提交表单后,控制器中的数据将为空。但是如果对语句使用
,我们可以得到提交的模型。是的,我先测试了foreach,但没有工作,很奇怪。
@model List<netcore3_1sample.Models.Item>

@{
    ViewData["Title"] = "ItemIndex";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
 
<h2 class="text-center text-success">@ViewBag.name İsimli Araca Yükleme Yap</h2>

<form asp-controller="Home"
      asp-action="LoadItem" method="post">
    <br /> 
    <input type="submit" name="submit" value="Oluştur" class="btn btn-primary" />
    <br />
    <table class="table table-bordered table-striped table-hover">
        <thead>
            <tr>
                <th class="text-center">
                    Yüklensin mi?
                </th>
                <th class="text-center">
                    Name
                </th>
                <th class="text-center">
                    Price
                </th>
                <th class="text-center">
                    Description
                </th>
            </tr>
        </thead>
        <tbody> 
            @for( var i = 0; i < Model.Count;i++)
            {
                <tr>
                    <td>
                        <div class="form-check">
                            <label class="form-check-label" for="@Model[i].Id" asp-validation-for="@Model[i].Id"></label>
                            <input class="form-check-input" name="ids" type="checkbox" value="@Model[i].Id" id="@Model[i].Id">
                            @*<input type="checkbox" asp-for="@Model[i].IsChecked" />*@
                            <input type="hidden" asp-for="@Model[i].Id" />
                        </div>
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => Model[i].Name)
                        <input type="hidden" asp-for="@Model[i].Name" />
                    </td>
                    <td>
                        ₺@Html.DisplayFor(modelItem => Model[i].Price)
                        <input type="hidden" asp-for="@Model[i].Price" />
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => Model[i].Description)
                        <input type="hidden" asp-for="@Model[i].Description" />
                    </td>
                </tr>
            }
        </tbody>
    </table>
</form>
    [HttpPost]
    public ActionResult LoadItem(List<Item> model, int[] ids)
    {
        return RedirectToAction("ItemIndex");
    }
    <input class="form-check-input" name="ids" type="checkbox" value="@Model[i].Id" id="@Model[i].Id">
    <input type="checkbox" asp-for="@Model[i].IsChecked" />