【问题标题】:How to Write checkbox with foreach?如何用foreach编写复选框?
【发布时间】:2019-12-05 20:31:37
【问题描述】:

我有一个具有 IsChecked 属性的菜单列表。

我写了复选框列表并设置了 asp-for

@foreach (var a in Model.Menus.FindAll(x => x.Pid == item.ID))
                                        {
                                            <div class="form-check">
                                                <input class="form-check-input" type="checkbox" id="checkbox1" asp-for="@a.IsChecked" />
                                                <label for="checkbox1" class="form-check-label">@a.Name</label>
                                            </div>
                                        }

但在 OnPostAsync 函数中,菜单为空。我已经声明了菜单:public List&lt;Model.Menus&gt;Menus{get;set;} 并且 OnPostAsync 被激活。 我该怎么办?

【问题讨论】:

    标签: razor model-view-controller .net-core


    【解决方案1】:

    以下是在您的收藏中使用asp-for 标签助手的方法:

    @{ 
        var index = 0;
        // Required or the index won't match
        Model.Menus = Model.Menus.FindAll(x => x.Pid == item.ID).ToList();
    }
    @foreach (var a in Model.Menus)
    {
        <div class="form-check">
            <input class="form-check-input" type="checkbox" asp-for="@Model.Menus[index].IsChecked" />
            <label asp-for="@Model.Menus[index].IsChecked" class="form-check-label">@a.Name</label>
            <input type="hidden" asp-for="@Model.Menus[index].Id" />
            <input type="hidden" asp-for="@Model.Menus[index].Name" />
        </div>
        index++;
    }
    

    【讨论】:

      猜你喜欢
      • 2015-08-06
      • 2020-03-01
      • 2021-01-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-09
      相关资源
      最近更新 更多