代码之家  ›  专栏  ›  技术社区  ›  Ege Bayrak

从列表创建和绑定razor复选框

  •  0
  • Ege Bayrak  · 技术社区  · 7 年前

    我写《剃刀》已经有一段时间了,我一直在创作 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 .所以我想我有一个有约束力的问题。

    2 回复  |  直到 7 年前
        1
  •  1
  •   Umer Zaman    7 年前
           @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>
                        }
    

    这里有一个例子,我是如何做到的,在你的情况下也是这样

        2
  •  1
  •   Saadi    7 年前

    尝试使用 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 })
        }