Access to a Viewbag, how is an array?

Imagine a bag with a picture

 ViewBag.Modes

it contains the following:

Simple
Advanced
Manual
Complete

How can I access the viewbag by index, as in an array?

For example, Simple has an index of 0, then it will look like

ViewBag.Modes[0]

I tried above, but it doesn’t work like that ... How can I replicate this using the viewbag or is there a workaround that I can use?

+4
source share
5 answers

Thanks for the posts, but soon after writing this, I came up with this solution using the model and list as shown below, it can also be done using the viewbag instead of the model.

List<string> list = new List<string>();
            foreach (Models.AppModeInfo blah in Model.theAppModes)
            {
                list.Add(blah.Name);
            }

            var AppModeText = "";
            switch (item.AppModeId)
            {
                case 1:
                    AppModeText = list[0];
                    break;
                case 2:
                    AppModeText = list[1];
                    break;
                case 3:
                    AppModeText = list[2];
                    break;
                case 4:
                    AppModeText = list[3];
                    break;
                case 5:
                    AppModeText = list[4];
                    break;
            }
0
source

ViewBag is a dynamic property that complicates things a bit.

, , ViewData. ViewData - , , Linq:

this.ViewData.Keys.ElementAt(0);
+4

This does the trick for me:

Controller:

public ActionResult Index()
{
    var stringArray = new string[3] { "Manual", "Semi", "Auto"};
    ViewBag.Collection = stringArray;
    return View();
}

View:

    @foreach(var str in ViewBag.Collection)
    {
        @Html.Raw(str); <br/>
    }

    @for (int i = 0; i <= 2; i++ )
    {
        @Html.Raw(ViewBag.Collection[i]) <br/>
    }

Conclusion:

enter image description here

Sorry for not using your terms. I scripted this with my head.

+3
source

Using ViewBag:

controller

public ActionResult Index()
{
List<string> modes = new List<string>();
modes.Add("Simple");
modes.Add("Advanced");
modes.Add("Manual");
modes.Add("Complete");
ViewBag["Modes"] = modes;
return View();
}

View

<h1>List of Modes</h1>
@{foreach (var mode in ViewBag.Modes) {
    <li>
        @hobby
    </li>
} }

----------------------------------------------- --- --------------

Using ViewData:

controller

public ActionResult Index()
{
    string[] Modes = {"Simple", "Advanced", "Manual", "Complete" };
    ViewData["Modes"] = Modes;
    return View();
}

** View

<h1>List of Modes</h1>
@foreach (string mode in ViewData["Modes"] as string[]) {
    <li>
        @mode
    </li>
}
0
source

In your controller:

string[] Modes = {"Simple", "Advanced", "Manual", "Complete" };
ViewData["Modes"] = Modes;

In your opinion:

<div>
    @(((string[])ViewData["Modes"])[0]) 
</div>
<div>
    @(((string[])ViewData["Modes"])[1]) 
</div>
<div>
    @(((string[])ViewData["Modes"])[2]) 
</div>
0
source

All Articles