【发布时间】:2020-11-21 20:58:29
【问题描述】:
我正在尝试在表格中输出玩家的统计数据。并非所有玩家都会根据当天的统计数据。我尝试了其他方法,但所有人仍在抱怨。这是我现在拥有的代码:
<tbody>
@foreach(var player in @ViewBag.Roster){
int index = 0;
<tr>
<td>@player.Name, @player.TeamName @player.Position</td>
if(@ViewBag.Stats[index] == null){
<td>--</td>
<td>--</td>
<td>--</td>
<td>--</td>
}
else{
<td>@ViewBag.Stats[index].Points</td>
<td>@ViewBag.Stats[index].Rebounds</td>
<td>@ViewBag.Stats[index].Assists</td>
<td>@ViewBag.Stats[index].Turnovers</td>
}
</tr>
index++;
}
</tbody>
异常详细信息:Microsoft.CSharp.RuntimeBinder.RuntimeBinderException:无法对空引用执行运行时绑定
来源错误:
第 32 行:}
第 33 行:否则{
第 34 行:@ViewBag.Stats[index].Points
第 35 行:@ViewBag.Stats[index].Rebounds
第 36 行:@ViewBag.Stats[index].Assists
【问题讨论】:
-
无法理解您面临的问题是什么?
-
我正在尝试检查每个玩家是否有统计数据。如果他们这样做,我会输出它。否则只输出'--'。也许有不同的语法来检查统计信息是否存在?
-
是的!那行得通,但现在我的 index++ 不起作用。任何想法我应该把它放在哪里?它只是为我的所有玩家输出相同的统计数据。
-
好吧,我不知道它是否解决了我最初的问题,即如果 stat 为空,则输出'--'。但是页面加载没有错误
-
我已经发布了关于索引问题的答案..
标签: c# asp.net asp.net-mvc