[英]ASP.NET MVC 4 - Cannot perform runtime binding on a null reference
I am trying to output a player's stats in a table.我试图在表格中输出玩家的统计数据。 Not all players will have stats depending on the day.并非所有玩家都会根据日期获得统计数据。 I have tried other ways and all are still complaining.我尝试了其他方法,但所有人仍在抱怨。 Here is the code I have now:这是我现在拥有的代码:
<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>
Exception Details: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: Cannot perform runtime binding on a null reference异常详细信息:Microsoft.CSharp.RuntimeBinder.RuntimeBinderException:无法对空引用执行运行时绑定
Source Error:源错误:
Line 32: }第 32 行:}
Line 33: else{第 33 行:其他{
Line 34: @ViewBag.Stats[index].Points第 34 行:@ViewBag.Stats[index].Points
Line 35: @ViewBag.Stats[index].Rebounds第 35 行:@ViewBag.Stats[index].Rebounds
Line 36: @ViewBag.Stats[index].Assists第 36 行:@ViewBag.Stats[index].Assists
OK I am posting the full answer here -好的,我在这里发布完整的答案 -
Try @
before if(@ViewBag.Stats[index] == null){
and remove @
from @ViewBag
inside the if
so that it look like this - @if(ViewBag.Stats[index] == null){
尝试@
之前if(@ViewBag.Stats[index] == null){
并删除@
从@ViewBag
内if
这样它看起来就像这样- @if(ViewBag.Stats[index] == null){
You are setting index = 0
, inside foreach
, so it is initialised in every loop.您在foreach
内部设置index = 0
,因此它在每个循环中初始化。 Initialise it outside foreach
like this像这样在foreach
之外初始化它
var index = 0; foreach ...
if you are facing problem for the scope try this -如果您遇到范围问题,请尝试此操作-
@{
var index = 0;
foreach (....) {
.......
index++
}
}
if you want to keep track of the index, why dont you rewrite your loop as:如果您想跟踪索引,为什么不将循环重写为:
var obj = new string[] { "", "", "" };
for(var index = 0; index < obj.Length; index++)
{
var item = obj[index];
/* DO STUFF WITH ITEM */
}
foreach(var item in obj.Select((value, index) => new { index, value }))
{
/* DO STUFF WITH item.Value */
}
I tried both cases suggested above, but with no luck.我尝试了上面建议的两种情况,但没有运气。 Then finally I had to initialize my ViewBag collection in the Controller's Index method itself.最后,我不得不在控制器的 Index 方法本身中初始化我的 ViewBag 集合。 So when I did something like this,所以当我做这样的事情时,
var stringArray = new string[10] { "", "", "", "", "", "", "", "", "", "" };
ViewBag.doc = stringArray;
it worked.有效。 (I had 10 elements. If they were more, had to do something else for initialization.) My code on the view part is as follows now: (我有 10 个元素。如果它们更多,则必须为初始化做一些其他事情。)我在视图部分的代码现在如下:
@for (var itm =0; itm< 10; itm++)
{
<div class="form-group">
<label asp-for="Documents[itm].doc"></label>
<input type="file" asp-for="Documents[itm].doc" />
@if (ViewBag.doc[itm] != "")
{<a asp-area="" asp-page="/wwwroot/CaseDocuments/@ViewBag.doc[itm]">@ViewBag.doc[itm]</a>
<label asp-for="Documents[itm].remove"></label>
@Html.CheckBoxFor(a => a.Documents[itm].remove)
}
</div>
<div class="form-group">
<label asp-for="Documents[itm].desc"></label>
<textarea asp-for="Documents[itm].desc" class="form-control" rows="3"></textarea>
</div>
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.