Site icon Dotnet Helpers

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 :

Exit mobile version