0

I have some trouble understanding checkboxes in 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.

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.

I googled a lot but couldn't find a proper Explanation for the use checkboxes in 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;
        }
    }
Mr. Paul
  • 103
  • 3
  • 16
  • A checkbox binds to a `bool` property. Show you model and indicate which property you want to bind to –  Feb 03 '16 at 08:52
  • I want too bind to the objects Person, give me a second to edit – Mr. Paul Feb 03 '16 at 08:53
  • You can use @Html.CheckBoxFor. More details, please go to link [http://stackoverflow.com/questions/12674572/proper-usage-of-net-mvc-html-checkboxfor](http://stackoverflow.com/questions/12674572/proper-usage-of-net-mvc-html-checkboxfor) – Nguyen Feb 03 '16 at 08:55
  • Create a new ViewModel which will contain a List of objects of type Person and also will contain a flag for each person which will indicate whether the person is enabled or not (I am just guessing here, because you didn't share why you need the checkbox). Then you use the newly created ViewModel in your View and use @Html.CheckBoxFor for the flag and whatever you want for the Person object. – Norbert Szenasi Feb 03 '16 at 09:03
  • Create a `PersonViewModel` with the properties of `Person` that you want to display/edit and an addition `bool IsSelected` property and use a `for` loop or custom `EditorTemplate` to generate the items in the collection –  Feb 03 '16 at 09:20

1 Answers1

-1

First of define any bool property in your person model. e.g.

public bool Ischecked { get; set; }

In View

@foreach (var element in Model)
{
  <p>@Html.CheckBoxFor(m => element.Ischecked) @Html.DisplayFor(m => element.Name)</p>
}
Utsav
  • 16
  • 3
  • You cannot use a `foreach` loop to generate form controls for collection properties - apart from the invalid html its creating duplicate `name` attributes which have no relationship to the model and will not bind. –  Feb 03 '16 at 09:19