Reputation: 5670
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.
GameID
GameDateTimeDisplay
GameTeamDisplay
WinningTeamDisplay
PickedTeamAbbr
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.
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.
Anyway, here is my code. The line that is causing the exception is this: @gameID = @pick.Game.GameID;
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.
<table>
<thead>
<tr>
<th>Game</th>
<th>Game Date/Time</th>
<th>Winner</th>
@foreach(var name in @Model.UserNames) {
<th>@name</th>
}
</tr>
</thead>
<tbody>
@{
int lastGameID = 0;
int gameID = 0;
bool firstGame = true;
}
@foreach(var pick in @Model.Picks) {
@gameID = @pick.Game.GameID;
if(@gameID != @lastGameID) {
if(!@firstGame){
<text>@Html.Raw("</tr>")</text>
}
@Html.Raw(
<tr>
<td>@pick.GameTeamDisplay</td>
<td>@pick.GameDateTimeDisplay</td>
<td>@pick.Game.WinningTeam.TeamAbbr</td>
<td>@pick.PickedTeamAbbr</td>
)
}
else {
@Html.Raw(<td>@pick.PickedTeamAbbr</td>)
}
}
@Html.Raw(</tr>)
</tbody>
</table>
UPDATE: 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;}
Compilation Error: CS1501: No overload for method 'Write' takes 0 arguments
Here is the updated Code:
@foreach(var pick in @Model.Picks) {
@{gameID = @pick.Game.GameID;}
if(@gameID != @lastGameID) {
if(!@firstGame){
@:</tr>
}
@:<tr>
<td>@pick.GameTeamDisplay</td>
<td>@pick.GameDateTimeDisplay</td>
<td>@pick.Game.WinningTeam.TeamAbbr</td>
<td>@pick.PickedTeamAbbr</td>
}
else {
<td>@pick.PickedTeamAbbr</td>
}
}
</tr>
Upvotes: 3
Views: 11916
Reputation: 5670
I determined that my Razor view code was simply too complex. The real problem was that I was trying to force a view to work with a Model that I had created for another view. I ended up creating a few more models specifically for this view. Code is much cleaner and best of all it works! Here is the code I ended up with.
<table>
<thead>
<tr>
<th>Game</th>
<th>Game Date/Time</th>
<th>Winner</th>
@foreach(var name in @Model.UserNames) {
<th>@name</th>
}
</tr>
</thead>
<tbody>
@foreach(var game in @Model.Games) {
<tr>
<td>@game.GameDescription</td>
<td>@game.GameDisplayDateTime</td>
<td>@game.GameWinner</td>
@foreach(var pick in game.GamePicks){
<td>@pick.PlayerPick</td>
}
</tr>
}
</tbody>
</table>
Upvotes: 2
Reputation: 2027
Just write
@foreach(var pick in Model.Picks) {
<tr>
<td>@pick.GameTeamDisplay</td>
<td>@pick.GameDateTimeDisplay</td>
<td>@pick.Game.WinningTeam.TeamAbbr</td>
<td>@pick.PickedTeamAbbr</td>
</tr>
}
You don't need @
inside code block. You can use @:
instead of <text>, Html.Raw
You can see here Razor syntax reference
Upvotes: 4
Reputation: 12447
You need to surround it with {
}
to make it a codeblock
@{gameID = pick.Game.GameID;}
Also, you don't need the @
inside the foreach
/if
statements because they're code blocks.
e.g. you could just write:
foreach(var name in Model.UserNames) {
Upvotes: 4