0

This is my simple class:

public class Friends
{
  public Friends()
  {
    Myfriend = new List<string>();
    Share = new List<bool>();
  }
  public List<string> Myfriend { get; set; }
  public List<bool> Share { get; set; }
}

This is the class i want to pass into my view. Before I do that I would like to "fill" up

public List<string> Myfriend { get; set; }

With the values i have in another list of strings.

var listofUsersFriends <--- This list contains a bunch of names(strings).

Can I assign the values i a way similar to this:

var model = new Friends();
foreach (var item in listofUsersFriends)
{
  //Add list items
}

I'm totally stuck, feel free to give advice on other ways of solving this =) Thanks!

EDIT: The end goal is to pass a model to my view containing a list friend-objects. Like this: Bob CheckboxShare Wouter CheckboxShare ETC..

This seems to send a model with two separate strings, i.e the checkbox is not related to the friend. I know I have messed up somewhere but i do not understand how to solve it.

tereško
  • 58,060
  • 25
  • 98
  • 150
user2915962
  • 2,691
  • 8
  • 33
  • 60

1 Answers1

0

You have a couple of options:

  • Add each item individually to the list in your foreach like this:

    model.Friends.Add(item);

  • Initialize a new list based on your listofUsersFriends :

    model.Friends = new List(listofUsersFriends);

  • Add a range of items in one go:

    model.Friends.AddRange(listofUsersFriends);

Update

To link the booleand and string field together you can declare a class that defines your data:

public class FriendStatus
{
    public string Name { get; set; }
    public bool Checked get; set; }
}

And then change your view model to have a list of FriendStatus.

Wouter de Kort
  • 39,090
  • 12
  • 84
  • 103