title: MVC3 Razor foreach problems
body: <p>I've got a model that is returning a IEnumerable of football picks for a variable number of users. Due to this, I'm dynamically building an html table with a variable number of columns.  Basically, my picks will come back like this.  However, each game is going to be repeated for each User, so I'm only adding the first 3 columns of each table row once, and then adding only a single  tag until the gameID changes.  I know there are probably better ways to do this, but it's just a side project that I need to get done quickly.  And, I just want to figure out why it's not working.  </p>
  
  <pre><code>    GameID
      GameDateTimeDisplay
      GameTeamDisplay
      WinningTeamDisplay
      PickedTeamAbbr
  </code></pre>
  
  <p>OK, so here is the insanity that doesn't work.  I can get my table headers created successfully, but then the tbody isn't working, but it's erroring in an odd place.  </p>
  
  <p>I had to put all the @Html.Raw(...) stuff in there because it was having trouble finding the end tags for the foreach and if statements without them.</p>
  
  <p>Anyway, here is my code.  The line that is causing the exception is this:
          @gameID = @pick.Game.GameID;</p>
  
  <p>The exception is --> Compiler Error Message: CS1525: Invalid expression term '='
  The intellisense shows @gameID as a variable and the @pick.Game.GameID seems to be correct as well.</p>
  
  <pre><code>&lt;table&gt;
          &lt;thead&gt;
              &lt;tr&gt;
              &lt;th&gt;Game&lt;/th&gt;
              &lt;th&gt;Game Date/Time&lt;/th&gt;
              &lt;th&gt;Winner&lt;/th&gt;
              @foreach(var name in @Model.UserNames) { 
                  &lt;th&gt;@name&lt;/th&gt;   
              }
              &lt;/tr&gt;
          &lt;/thead&gt;
          &lt;tbody&gt;
              @{
              int lastGameID = 0;
              int gameID = 0;
              bool firstGame = true;
              }
  
              @foreach(var pick in @Model.Picks) {
                  @gameID = @pick.Game.GameID;
                  if(@gameID != @lastGameID) { 
                      if(!@firstGame){
                          &lt;text&gt;@Html.Raw("&lt;/tr&gt;")&lt;/text&gt;
                      }
                      @Html.Raw(
                      &lt;tr&gt; 
                          &lt;td&gt;@pick.GameTeamDisplay&lt;/td&gt;
                          &lt;td&gt;@pick.GameDateTimeDisplay&lt;/td&gt;
                          &lt;td&gt;@pick.Game.WinningTeam.TeamAbbr&lt;/td&gt;
                          &lt;td&gt;@pick.PickedTeamAbbr&lt;/td&gt;             
                      )
  
                  }
                  else {
                      @Html.Raw(&lt;td&gt;@pick.PickedTeamAbbr&lt;/td&gt;)
                  }
              }    
              @Html.Raw(&lt;/tr&gt;)
  
          &lt;/tbody&gt;
      &lt;/table&gt;
  </code></pre>
  
  <p><strong>UPDATE:</strong>
  I've removed the @Html.Raw, , etc... Also wrapped the gameID assignment in a @{}.  It's now giving me an error on this line, 
          @{gameID = @pick.Game.GameID;}</p>
  
  <p>Compilation Error: CS1501: No overload for method 'Write' takes 0 arguments</p>
  
  <p>Here is the updated Code:</p>
  
  <pre><code>    @foreach(var pick in @Model.Picks) {
          @{gameID = @pick.Game.GameID;}
          if(@gameID != @lastGameID) { 
              if(!@firstGame){
                  @:&lt;/tr&gt;
              }
  
              @:&lt;tr&gt; 
                  &lt;td&gt;@pick.GameTeamDisplay&lt;/td&gt;
                  &lt;td&gt;@pick.GameDateTimeDisplay&lt;/td&gt;
                  &lt;td&gt;@pick.Game.WinningTeam.TeamAbbr&lt;/td&gt;
                  &lt;td&gt;@pick.PickedTeamAbbr&lt;/td&gt;             
          }
          else {
              &lt;td&gt;@pick.PickedTeamAbbr&lt;/td&gt;
          }
      }    
      &lt;/tr&gt;
  </code></pre>