Home > Backend Development > C++ > How to Efficiently Bind a List of Checkboxes to a Model in ASP.NET MVC?

How to Efficiently Bind a List of Checkboxes to a Model in ASP.NET MVC?

Mary-Kate Olsen
Release: 2025-01-31 17:36:13
Original
577 people have browsed it

How to Efficiently Bind a List of Checkboxes to a Model in ASP.NET MVC?

Pass the re -election box list in the ASP.NET MVC view and extract IenuMerable

In ASP.NET MVC, the collection of the check box list to the model is essential for collecting user preferences. However, when you have the existing option list and you need to display the associated options, the default ForeACH cycle method seems to be powerless.

In order to solve this problem, please consider using a strong type HTML auxiliary method. They provide seamless two -way model binding to avoid the need to manually generate HTML.

Create a view model

First, create a view model to represent the data in the view. In this example, you associate the user with the available character. You can define ROLEVM and Uservm:

Fill in the charging view model

public class RoleVM
{
  public int ID { get; set; }
  public string Name { get; set; }
  public bool IsSelected { get; set; }
}

public class UserVM
{
  public int ID { get; set; }
  public string Name { get; set; }
  public List<RoleVM> Roles { get; set; }
}
Copy after login

In the GET operation associated with the view, use user data to fill the Uservm, including the role that can be used by the role list and marked association:

<视> Show the check box in the view

public ActionResult Edit(int ID)
{
  UserVM model = new UserVM();
  User user = db.Find(ID);  // 数据库查找方法示例
  model.ID = user.ID;
  model.Name = user.Name;
  // 填充角色并根据用户角色设置其IsSelect属性
  foreach (Role role in db.Roles)  // 数据库查找方法示例
  {
    model.Roles.Add(new RoleVM
    {
      ID = role.ID,
      Name = role.Name,
      // 判断用户是否与角色关联
      IsSelected = user.Roles.Any(ur => ur.RoleID == role.ID)
    });
  }
  return View(model);
}
Copy after login
In the view, use the HTML auxiliary method to present the check box for each character in Uservm:

<集> Collect users to choose

In the POST operation associated with the form, the binding model will be binded, and the updated ISSELECTED attribute will reflect the user's choice:
@for (int i = 0; i < Model.Roles.Count; i++)
{
  @Html.HiddenFor(m => m.Roles[i].ID)
  @Html.CheckBoxFor(m => m.Roles[i].IsSelected)
  @Html.LabelFor(m => m.Roles[i].IsSelected, Model.Roles[i].Name)
}
Copy after login

This method ensures the correct model binding, and at the same time allows users to choose from the complete option list and associate the selected options with the user.

The above is the detailed content of How to Efficiently Bind a List of Checkboxes to a Model in ASP.NET MVC?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template