从枚举中填充复选框列表

本文关键字:复选框 列表 填充 枚举 | 更新日期: 2023-09-27 18:11:39

我已经创建了一个公共enum Interests

public enum Interests
{
    Sport,
    Party,
    Gaming   
}

并希望在我的用户配置文件中有一个枚举列表

public class User
{
    public string Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Email { get; set; }
    public List<Interests> Interests = new List<Interests>;
}

我想为我的枚举值创建一个复选框表单,它最终应该存储在我的列表中,但无法找到如何…

我想有每个爱好在enum兴趣一个Checkbox- field和每个选中的复选框应该存储在列表中。

从枚举中填充复选框列表

Try

checkboxID.DataSource = Enum.GetNames(typeof(Interests));
checkboxID.DataBind();
为MVC:

@Html.EnumDropDownListFor(m => m.Interests)在视图