3

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

2
  • If you need to post you values/form back to the server, which I think you do, the second method is the correct one, i.e. using for. However, without you error message, is hard to tell what the problem is. Commented Aug 16, 2014 at 12:57
  • Yes i need to post the values back to the server, the error in the For-loop is: Argument type "bool" is not assignable to parameter type system linq expression expressions<system func<notify_core.models.NotifyUser.bool> Commented Aug 16, 2014 at 13:01

1 Answer 1

7

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)
} 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.