简体   繁体   English

用剃刀将复选框添加到asp.net中的对象

[英]Adding checkbox to object in asp.net with razor

I have some trouble understanding checkboxes in asp.net MVC. 我在理解asp.net MVC中的复选框时遇到一些麻烦。 I am trying to display a list of objects and adding a checkbox to each object. 我试图显示对象列表,并向每个对象添加一个复选框。 I just don't know which kind of checkbox to use. 我只是不知道要使用哪种复选框。

I created checkboxes with usual html or razors @html.checkbox/checkboxfor but I don't know how to connect them to the object. 我用普通的html或razors @ html.checkbox / checkboxfor创建了复选框,但是我不知道如何将它们连接到对象。

I want to choose some of the listed objects and in a second step display only those. 我想选择一些列出的对象,然后在第二步中仅显示那些对象。

Can someone explain to me which checkbox I should use and how to connect them to the object? 有人可以告诉我应该使用哪个复选框以及如何将其连接到对象吗?

The objects have an id with which we could connect the Checkbox. 这些对象具有一个ID,我们可以使用该ID连接Checkbox。

I googled a lot but couldn't find a proper Explanation for the use checkboxes in asp.net mvc. 我在Google上搜索了很多,但是在asp.net mvc中找不到使用复选框的正确解释。

View 视图

@model List<Person> 
@{
    ViewBag.Title = "ShowView";
}

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

Model 模型

    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;
        }
    }

First of define any bool property in your person model. 首先在您的个人模型中定义任何布尔属性。 eg 例如

public bool Ischecked { get; set; }

In View 视野中

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM