Home/MVC/store array in viewbag and retrieving in the view

store array in viewbag and retrieving in the view

In this post, we will discuss about how to store the array in view bag and how to use them in view.

Controller

A simple Controller action which store the string array in the ViewBag and return to the view.

public ActionResult Index()
{
string[] Countries = {“India”, “US”,”Nepal” };
ViewBag.Countries = Countries;
return View();
}

View

<h2>List Of Cities</h2>
@foreach (string city in ViewBag.Countries) {
<li>@city
</li>
}

OUTPUT :

4 Comments

  1. Anonymous May 6, 2013 at 6:55 pm

    very pleased to be here.

  2. riparazione wifi grigio August 8, 2014 at 2:09 am

    I like your blog. Bookmarked.

  3. Stephen S September 30, 2014 at 10:02 am

    It is very helpful , thank a lot

  4. Kirsten Neighbour August 28, 2017 at 5:22 pm

    Hello Dear, are you genuinely visiting this site daily, if so after that you will absolutely obtain pleasant knowledge.

Leave A Comment