MVC3 Razor foreach问题
我有一个模型,它为可变数量的用户返回 IEnumerable 的足球选秀权。因此,我动态构建了一个具有可变列数的 html 表。基本上,我的选择会像这样回来。但是,每个游戏都会为每个用户重复,因此我只添加每个表行的前 3 列一次,然后仅添加一个标签,直到游戏 ID 发生变化。我知道可能有更好的方法来做到这一点,但这只是我需要快速完成的一个副项目。而且,我只是想弄清楚为什么它不起作用。
GameID
GameDateTimeDisplay
GameTeamDisplay
WinningTeamDisplay
PickedTeamAbbr
好吧,这就是行不通的疯狂之处。我可以成功创建表头,但 tbody 无法工作,但在一个奇怪的地方出错。
我必须将所有 @Html.Raw(...) 内容放在那里,因为在没有它们的情况下很难找到 foreach 和 if 语句的结束标记。
无论如何,这是我的代码。导致异常的行是这样的: @gameID = @pick.Game.GameID;
例外是 -->编译器错误消息:CS1525:无效的表达式术语“=” 智能感知将 @gameID 显示为变量,并且 @pick.Game.GameID 似乎也是正确的。
<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>
更新: 我删除了 @Html.Raw 等...还将 gameID 分配包装在 @{} 中。现在这行给我一个错误, @{gameID = @pick.Game.GameID;}
编译错误:CS1501:方法“Write”没有重载需要 0 个参数
以下是更新后的代码:
@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>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要用
{
}
将其包围以使其成为代码块此外,您不需要
foreach< 内的
@
/code>/if
语句,因为它们是代码块。例如你可以写:
You need to surround it with
{
}
to make it a codeblockAlso, you don't need the
@
inside theforeach
/if
statements because they're code blocks.e.g. you could just write:
只需在代码块中写入
You don't need, Html.Raw
@
即可。您可以使用@:
代替您可以在这里看到 Razor 语法参考
Just write
You don't need
@
inside code block. You can use@:
instead of<text>, Html.Raw
You can see here Razor syntax reference
我确定我的 Razor 视图代码太复杂了。真正的问题是我试图强制一个视图与我为另一个视图创建的模型一起工作。我最终专门为此视图创建了更多模型。代码更加简洁,而且最重要的是它可以工作!这是我最终得到的代码。
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.