'Can I use ViewData using 'for-loop' for int array?

namespace ViewDataDemo.Controllers
{
    public class HomeController: Controller
    {
        // GET: Home
        public ActionResult Index()
        {
            ViewData["Message"] = "Hello World!";
          

            int[] Numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
            ViewData["Numbers"] = Numbers;

            return View();
        }
    }
}


Solution 1:[1]

Yes, you can.

You can save any object there, including arrays.

However when you access the array, you have to castto ìnt[], like this:

int a =  ((ìnt[])ViewData["Numbers"])[i];

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Poul Bak