6

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

Source Error:

Line 32: }

Line 33: else{

Line 34: @ViewBag.Stats[index].Points

Line 35: @ViewBag.Stats[index].Rebounds

Line 36: @ViewBag.Stats[index].Assists

5
  • Can't understand what is the problem you are facing? Commented May 4, 2014 at 18:39
  • I am trying to check if each player has a stat. If they do I will output it. Else just output '--'. Maybe there is a different syntax for checking if a stat exists? Commented May 4, 2014 at 18:48
  • Yes! That worked, but now my index++ is not working. Any ideas where I should put it? It just output the same stats for all my players. Commented May 4, 2014 at 18:59
  • Well I don't know if it solved my original problem of outputting the '--' if the stat is null. But the page loaded with no errors Commented May 4, 2014 at 19:04
  • I have posted an answer for the problem with index.. Commented May 4, 2014 at 19:06

3 Answers 3

9

OK I am posting the full answer here -

  1. 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){

  2. You are setting index = 0, inside foreach, so it is initialised in every loop. Initialise it outside foreach like this

    var index = 0; foreach ...

if you are facing problem for the scope try this -

@{
    var index = 0;
    foreach (....) {
        .......
        index++
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Great, this made me check my *.cshtml again and I found a reference to a model that does not exist after I made a copy/paste from another code (using Razor syntax), something like '@Model.item' at the end of my code. Just commenting that line of code works for me.
0

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 */
    }

Comments

0

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. 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:

  @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>
                        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.