Reputation: 2711
These are my two classes:
public class NotifyUser : ContentPage
{
public NotifyUser()
{
Namn = "";
Friends = new List<FriendStatus>();
}
public string Namn { get; set; }
public List<FriendStatus> Friends { get; set; }
}
public class FriendStatus
{
public FriendStatus()
{
Name = "";
Checked = false;
}
public string Name { get; set; }
public bool Checked { get; set; }
}
In my view I try to loop through the NotifyUser-class, displaying the name-property and add an checkboxFor/editorFor the Checked-class:
Here are two ways i´ve tried:
@foreach (var friend in Model.Friends)
{
@Html.Editor(frien.Name)
@Html.EditorFor(frien.Checked)
}
@for (int i = 0; i < Model.Friends.Count; i++)
{
<p>@Model.Friends[i].Name</p>
@Html.CheckBoxFor(@Model.Friends[i].Checked)
}
Both of these gives me errors, surely there must be a pretty easy way to loop throuhg a list and have a checkbox for a bool? Thank you
Upvotes: 3
Views: 2161
Reputation: 7508
The CheckBoxFor
extension method expects as parameter an expression which returns Boolean
. Your example sets the Boolean
. Change it like this instead:
@for (int i = 0; i < Model.Friends.Count; i++)
{
<p>@Model.Friends[i].Name</p>
@Html.CheckBoxFor(x => @Model.Friends[i].Checked)
}
Upvotes: 7