In this article I will explain with an example, how to pass (send) List from Controller to View using ViewBag in ASP.Net MVC Razor.
The List Collection will be populated from Database using ADO.Net inside the Controller and it will be passed to the View using ViewBag.
Later in View, the List Collection will be iterated and the data will be displayed with the help of HTML Table in ASP.Net MVC Razor.
Note: For beginners in ASP.Net MVC, please refer my article ASP.Net MVC Hello World Tutorial with Sample Program example.
 
 
Database
I have made use of the following table Customers with the schema as follows. CustomerId is an Auto-Increment (Identity) column.
Pass (Send) List from Controller to View using ViewBag in ASP.Net MVC
 
I have already inserted few records in the table.
Pass (Send) List from Controller to View using ViewBag in ASP.Net MVC
 
Note: You can download the database table SQL by clicking the download link below.
          Download SQL file
 
 
Model
The Model class has the following properties.
public class CustomerModel
{
    public string CustomerId { get; set; }
    public string Name { get; set; }
    public string Country { get; set; }
}
 
 
Namespaces
You will need to import the following namespaces.
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
 
 
Controller
The Controller consists of the Index Action method. Inside this Action method, the records are fetched from the Customers Table using ADO.Net.
The records are inserted into the List Collection using SqlDataReader class object. Finally the List Collection is passed to the View using ViewBag.
public class HomeController : Controller
{
    // GET: Home
    public ActionResult Index()
    {
        List<CustomerModel> customers = new List<CustomerModel>();
        string constr = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constr))
        {
            string query = "SELECT * FROM Customers";
            using (SqlCommand cmd = new SqlCommand(query))
            {
                cmd.Connection = con;
                con.Open();
                using (SqlDataReader sdr = cmd.ExecuteReader())
                {
                    while (sdr.Read())
                    {
                        customers.Add(new CustomerModel
                        {
                            CustomerId = sdr["CustomerId"].ToString(),
                            Name = sdr["Name"].ToString(),
                            Country = sdr["Country"].ToString()
                        });
                    }
                }
                con.Close();
            }
        }
 
        ViewBag.Customers = customers;
        return View();
    }
}
 
 
View
Inside the View, for displaying the records, an HTML Table is used. A loop (iteration) will be executed over the rows of the List Collection which will generate the HTML Table rows with the Customer records.
@using Pass_List_ViewBag_MVC.Models;
@{
    Layout = null;
}
 
<!DOCTYPE html>
 
<html>
<head>
    <meta name="viewport" content="width=device-width"/>
    <title>Index</title>
</head>
<body>
    <table cellpadding="0" cellspacing="0">
        <tr>
            <th>CustomerId</th>
            <th>Name</th>
            <th>Country</th>
        </tr>
        @foreach (CustomerModel customer in (List<CustomerModel>)ViewBag.Customers)
        {
            <tr>
                <td>@customer.CustomerId</td>
                <td>@customer.Name</td>
                <td>@customer.Country</td>
            </tr>
        }
    </table>
</body>
</html>
 
 
Screenshots
Pass (Send) List from Controller to View using ViewBag in ASP.Net MVC
 
 
Downloads