【问题标题】:Creating and binding razor checkboxes from a list从列表中创建和绑定剃刀复选框
【发布时间】:2018-09-27 00:30:32
【问题描述】:

自从我写 razor 已经有一段时间了,我一直坚持从列表中创建 checkboxfor 选项并使其工作。我尝试了其他一些答案,但到目前为止没有一个为我解决这个问题。

这是我在视图中使用的模型。我正在尝试为角色创建复选框。

public class AccountRegisterModel
    {
        public string firstName { get; set; }
        public string lastName { get; set; }
        public string emailAddresses { get; set; }
        public string password { get; set; }
        public string passwordConfirm { get; set; }
        public bool? isactive { get; set; }
        public string phone { get; set; }
        public string subscriptionKey { get; set; }
        public string SalesPersonReference { get; set; }
        public int ProjectId { get; set; }
        public int SalesPersonId { get; set; }
        public int? SalesRegionId { get; set; }

        public List<SelectListModel> Roles { get; set; }

        public List<SelectListModel> Projects { get; set; }
        public List<SelectListModel> SalesRegions { get; set; }
        public int TenantID { get; set; }
        public Guid TenantGuid { get; set; }
        public string UserID { get; set; }
        public string UserEmail { get; set; }
    }

角色类型,SelectListModel

public class SelectListModel
    {
        public string Key { get; set; }
        public string Value { get; set; }
        public bool Selected { get; set; }
        public string ProjectHierarchyCode { get; set; }
        public string SalesRegionReference { get; set; }
        public int? Depth { get; set; }
        public bool isSelected { get; set; }
    }

我尝试创建复选框的方式

@foreach (var item in Model.Roles)
{
  @Html.CheckBoxFor(m=> item.isSelected, new { value = item.Value, id= "chkRole_"+item.Value})
}

模型“角色”列表被填充到控制器中,我在页面上看到了 9 个未命名的复选框。命名它们可以通过标签来解决,所以这不是问题。

问题是,当我选中某些框并点击“保存”时,选择的角色没有发布到控制器。模型参数中的角色字段来自null。所以我认为我有一个绑定问题。

【问题讨论】:

  • 你试过这种方式吗? @Html.CheckBoxFor(m =&gt; item.isSelected, new { value = item.Value, id= "chkRole_"+item.Value, Name = "CheckBox"})
  • 刚刚试了一下,Roles 还是空的。

标签: c# razor checkbox


【解决方案1】:

尝试使用For Loop。我认为 Razor HTML 渲染效果不佳的复选框列表。

您可以尝试以下方法:

for (int i = 0; i < Model.Roles.Count(); i++)
    {
        @Html.CheckBoxFor(m => Model.Roles[i].isSelected, new { value = Model.Roles[i].Value })
    }

【讨论】:

  • 这将所有角色发布到行动,但所有角色都是 isSelected = false 尽管我选择了其中两个
  • 您可能还需要为复选框更新隐藏字段的 ID。
  • 或者您可以从复选框中删除 id。
【解决方案2】:
       @for (int i = 0; i < Model.CampusTestResultModelList.Count; i++)
                    {
                        <tr>
                            <td> 
                              @Html.CheckBoxFor(modelItem=>Model.CampusTestResultModelList[i].RollNumber);

                            </td>
                            <td>
                                @Html.DisplayFor(modelItem => Model.CampusTestResultModelList[i].StudentName)
                            </td>
                            <td>
                              @Html.TextBoxFor(modelItem => Model.CampusTestResultModelList[i].ObtainedMarks, new { @type = "number", min=0, step =1 ,
                             @class = "numeric-class form-control"})


                              @Html.HiddenFor(modelItem => Model.CampusTestResultModelList[i].TotalMarks)
                            </td>
                        </tr>
                    }

这是我如何做的例子,在你的情况下是一样的

【讨论】:

    猜你喜欢
    • 2014-08-18
    • 2022-11-28
    • 1970-01-01
    • 2019-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-06
    • 2021-08-27
    相关资源
    最近更新 更多