0

I am taking an MVC tutorial that is teaching about ViewBags in ASP.NET. It never goes into what a ViewBag really is, it just shows you when and how to use it. I ran into the code below, and it seems to treat the ViewBag as an array in this particular instance, when I didn't think it is an array.

@for(int i = 0; i <= ViewBag.Items; i++)
    {
        <li>ViewBag.Items[i]</li>
    } 

What is ViewBag exactly? A data dictionary? A collection? An array? Seems like it is a dictionary from what I can research but I can't seem to find a clear answer, as the resources I found just tell you how and when to use it. I looked here, but the info was very sparse and not completely clear to me.

https://learn.microsoft.com/en-us/dotnet/api/system.web.mvc.controllerbase.viewbag?view=aspnet-mvc-5.2

Please note, my C# skills are not completely comprehensive yet although I have a decent general understanding. How can this be treated as an array?

Also, why is it i <= ViewBag.Items in the for loop instead of ViewBag.Items.Count? No explanation from instructor, he just uses it and never explains this discrepancy. Thank you for any assistance you can provide to clear my misunderstanding up.

Bobh
  • 321
  • 1
  • 14
  • 1
    hope this helps https://www.tutorialsteacher.com/mvc/viewbag-in-asp.net-mvc – MorenajeRD May 04 '19 at 13:06
  • 2
    This code run normally? – Hien Nguyen May 04 '19 at 13:09
  • It turns out, the instructor made an error, and then changed it to View,Items.Count later on, which makes much more sense. Lousy course. Errors like that can really through a newbie such as myself off. But I still don't know how this can be treated as an array. – Bobh May 04 '19 at 13:16
  • I read the provided tutorial link, and it's the same. It shows you how to use it, but doesn't really explain what it is. Since it's a wrapper for DataView, I assume it must be a dictionary. Can dictionaries be used like an array? – Bobh May 04 '19 at 13:21
  • Possible duplicate of [How ViewBag in ASP.NET MVC works](https://stackoverflow.com/questions/14896013/how-viewbag-in-asp-net-mvc-works) ViewBag is actually a System.Dynamic.ExpandoObject, so it explains all your questions. here is a simple example for you https://dotnetfiddle.net/ZyKGlM – Okan Kocyigit May 04 '19 at 13:22
  • Well I am a newbie to C# so I guess I need to read some more, I'll just delete the question since you said it's a duplicate and figure it out myself – Bobh May 04 '19 at 13:27
  • 1
    As I said ViewBag is a `System.Dynamic.ExpandoObject`, and in the example you share `ViewBag` is not used as an `array`, actually it's `Items` property is an `array`, I've created an example for you. https://dotnetfiddle.net/ZyKGlM – Okan Kocyigit May 04 '19 at 13:31
  • Thank you, please add that as an answer and I'll mark it as such. Items property as an array makes sense and that was what I was missing. Thx. – Bobh May 04 '19 at 13:33

1 Answers1

1

ViewBag is actually a System.Dynamic.ExpandoObject (source)

For the example you shared ViewBag is not used as an array, actually it's Items property is an array, Here is an example usage of System.Dynamic.ExpandoObject (ViewBag)

using System;
using System.Collections.Generic;

public class Program
{
    public static void Main()
    {
        dynamic ViewBag = new System.Dynamic.ExpandoObject();

        //Add a single string variable to ViewBag

        ViewBag.xxx = "Stackoverflow";
        Console.WriteLine(ViewBag.xxx);

        //Add a list to ViewBag
        ViewBag.Items = new List<int>() {10,20,30};

        for(var i = 0; i < ViewBag.Items.Count; i++) {
           Console.WriteLine(ViewBag.Items[i]);
        }
    }
}

Live Demo

Okan Kocyigit
  • 13,203
  • 18
  • 70
  • 129