0

I am a bit lost when it comes to querying arrays within arrays. My model comprises of the following:

(Arrangements) > (InterestedPlayers, JustPlayId) > (Name,Id)

In the code I have buttons triggering modals within this a foreach loop:

 foreach (var item in Model.Arrangements)
{
  ...
}

Now I want to list the Interested players' names with a checkbox for each one. So I tried this expression:

  Html.CheckBoxFor( m => m.Arrangements
                   .Where(x => x.JustPlayId == item.JustPlayId)
                   .Select(e => e.InterestedPlayers.Select(c => c.Name))
                  )

Any suggestions?

leppie
  • 115,091
  • 17
  • 196
  • 297
Jynn
  • 287
  • 1
  • 3
  • 18
  • "But it doesn't like this (squiggly red line), any suggestions" What does that mean? Do you get an exception? And which one is the "red line"? – MakePeaceGreatAgain Jun 23 '15 at 12:20
  • 2
    If you hover over the 'squiggly red line' what does it tell you? – sr28 Jun 23 '15 at 12:21
  • is it squiggly or wiggly ?! there's a difference ! – Noctis Jun 23 '15 at 12:22
  • 2
    This will not work. It expects the expression to be a simple member expression, not a LINQ query... – leppie Jun 23 '15 at 12:25
  • Check out this [question](http://stackoverflow.com/questions/12674572/proper-usage-of-net-mvc-html-checkboxfor) – juharr Jun 23 '15 at 12:28
  • leppie is right, and you need a join. https://msdn.microsoft.com/en-us/library/bb311040.aspx – Shashank Chaturvedi Jun 23 '15 at 12:47
  • As I was typing an answer, something didn't make sense. say you somehow generated the `player names` with `checkbox` for each. Will you be posting it to an action in your controller. if you `POST` it to an action, what didn't made sense to me is that there should be another variable for InterestedPlayer to mark it either checked or unchecked. Checkboxes post only the value, not the label part of it. – Amila Jun 23 '15 at 14:22

3 Answers3

0
try this.

 @foreach (var item in Model.Arrangements){
<div id="playersname">
 <%: item.InterestedPlayers.Name%>
 <%:Html.CheckBoxFor(model => Arrangements.InterestedPlayers.Name, new { id =     "InterestedPlayersname" })%>
 </div>

and use jquery to get all seletectd name of interested players

Wintergreen
  • 236
  • 1
  • 9
0

Html.CheckBoxFor needs to bind a bool property, not a string property like Name. You should create a bool property in your Model like IsSelect.

(Arrangements) > (InterestedPlayers, JustPlayId) > (Name,Id)

InterestedPlayers => Name(string), IsSelect(bool)

For example, try this:

int i = 0, j = 0;
foreach (var item in Model.Arrangements)
{
    foreach(var player in item.InterestedPlayers)
    {
        Html.CheckBoxFor(m => m.Arrangements[i].InterestedPlayers[j].IsSelect) 
        Html.LabelFor(m => m.Arrangements[i].InterestedPlayers[j].Name)
        j++;
    }
    j = 0;
    i++;
}    
  • thanks this sounds like a great solution and I see how I was trying to use checkboxfor incorrectly. – Jynn Jun 24 '15 at 12:30
0

Some good solutions have been posted above, but I thought I'd post what I came up with in the meantime, by using html.checkbox instead.

@using (Html.BeginForm("MoveToOverFlow", "JustPlay", new {id = Model.Id, slug = Model.Slug}))
{
foreach (var player in item.InterestedPlayers)
    {        
      @player.Name 
      @Html.CheckBox("PlayerId", new { value = player.Id})
      @Html.SubmitButton(">>>>>>",true,new {@class = "btn btn-primary"})
    }
}

This sends a list back to the controller which either has the id of the player if the checkbox is ticked or "false" if unticked

Jynn
  • 287
  • 1
  • 3
  • 18