Imagine a view bag called
ViewBag.Modes
this contains the following:
Simple
Advanced
Manual
Complete
How can I access the viewbag by index like you would in an array?
e.g Simple is at index 0 then it would look like this
ViewBag.Modes[0]
I tried the above but it doesn't work so...How can I replicate this with viewbag or is there a workaround I can use?
ViewBag is a dynamic property, which complicates things a bit.
For the behavior you're looking for, you might want to use ViewData instead. ViewData is a dictionary, which means you can access the values of each index using Linq:
Thanks for the posts but shortly after writing this I came up with this solution using a model & list as below this could also be done using viewbag instead of model.
In your controller:
In your View:
This does the trick for me:
Controller:
View:
Output:
Sorry for not using your terms. I was scrachting this together from the top of my head.
Using the ViewBag:
Controller
View
----------------------------------------------------------------
Using the ViewData:
Controller
**View