使用剃刀 asp.net 对象添加复选框

本文关键字:对象 添加 复选框 net asp 剃刀 | 更新日期: 2023-09-27 18:32:56

我在理解MVC中的复选框时遇到了一些麻烦 asp.net。我正在尝试显示对象列表并为每个对象添加一个复选框。我只是不知道要使用哪种复选框。

我用通常的html或剃须刀@html.checkbox/checkbox创建了复选框,但我不知道如何将它们连接到对象。

我想选择一些列出的对象,并在第二步中仅显示这些对象。

有人可以向我解释我应该使用哪个复选框以及如何将它们连接到对象吗?

这些对象有一个 id,我们可以用它连接复选框。

我用谷歌搜索了很多,但找不到 asp.net mvc 中使用复选框的正确解释。

视图

@model List<Person> 
@{
    ViewBag.Title = "ShowView";
}
@foreach (var element in Model)
{
    /*Checkbox*/ <p>@Html.DisplayFor(m => element.Name)</p>
}

    public class Person
    {
        public string Name { get; set; }
        public string Path { get; set; }
        public long Size { get; set; }
        public DateTime LastChange { get; set; }
        public int Id { get; set; }
        public Project(string name, string path, long size, DateTime lastChange, int id)
        {
            this.Name = name;
            this.Path = path;
            this.Size = size;
            this.LastChange = lastChange;
            this.Id = id;
        }
    }

使用剃刀 asp.net 对象添加复选框

首先定义人员模型中的任何布尔属性。

例如
public bool Ischecked { get; set; }

在视图中

@foreach (var element in Model)
{
  <p>@Html.CheckBoxFor(m => element.Ischecked) @Html.DisplayFor(m => element.Name)</p>
}