How To Show Dynamic List Using View Bag In ASP.NET MVC

Introduction

In this blog, we will learn about how we can show the dynamic list using View Bag in ASP.net MVC.

Step 1 - Create a Model

First create the Model and define the public properties of Model.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace AspNetMVCPractice.Models {
    public class Employee {
        public int Id {
            get;
            set;
        }
        public string Name {
            get;
            set;
        }
        public string Address {
            get;
            set;
        }
        public string Country {
            get;
            set;
        }
        public string City {
            get;
            set;
        }
        public Department Department {
            get;
            set;
        }
    }
}

Step 2 - Define the Action Method in Controller

public ActionResult Index() {
    return View();
}

Step 3 - Create the List and Pass the Model to that List

The third step is to create the list of Employees inside your Action Method.

public ActionResult Index() {
    List < Employee > list = new List < Employee > () {
        new Employee() {
                Id = 1,
                    Name = "Mudassar",
                    Address = "Abbottabad",
                    City = "Abbottabad Pakistan",
                    Country = "Paksistan"
            },
            new Employee() {
                Id = 2,
                    Name = "Asad",
                    Address = "Abbottabad",
                    City = "Abbottabad Pakistan",
                    Country = "Paksistan"
            },
            new Employee() {
                Id = 3,
                    Name = "Mubashir",
                    Address = "Abbottabad",
                    City = "Abbottabad Pakistan",
                    Country = "Paksistan"
            },
    };
    ViewBag.model = list;
    return View();
}

Step 4 - Pass the List to View bag

Pass the data of list to view bag using model,

List < Employee > list = new List < Employee > () {
    new Employee() {
            Id = 1,
                Name = "Mudassar",
                Address = "Abbottabad",
                City = "Abbottabad Pakistan",
                Country = "Paksistan"
        },
        new Employee() {
            Id = 2,
                Name = "Asad",
                Address = "Abbottabad",
                City = "Abbottabad Pakistan",
                Country = "Paksistan"
        },
        new Employee() {
            Id = 3,
                Name = "Mubashir",
                Address = "Abbottabad",
                City = "Abbottabad Pakistan",
                Country = "Paksistan"
        },
};
ViewBag.model = list;

Step 5 - Show the Data on Front End

Given below code is to show the dynamic list of data using view bag in asp.net MVC.

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<div class="wrapper">
  <div class="container">
    <table class="table">
      <thead>
        <tr>
          <th scope="col">Id</th>
          <th scope="col">Name</th>
          <th scope="col">Address</th>
          <th scope="col">City</th>
          <th scope="col">Country</th>
        </tr>
      </thead>
      @foreach (var item in ViewBag.model)             
       {                
      <tbody>
        <tr>
          <th scope="row">@item.Id</th>
          <td>@item.Name</td>
          <td>@item.Address</td>
          <td>@item.City</td>
          <td>@item.Country</td>
        </tr>
      </tbody>             }        
    </table>
  </div>
</div>

Output

How To Show Dynamic List Using View Bag