ASP.NET Core MVC - Pass Array From Controller To View

| Page Views: 212

In the previous example we introduced using Materializecss theme with our ASP.NET Core MVC webapp and switch through the menu items and switch through different pages. In this example we see how to pass data from Controller to the view. Specifically we pass a list or array and bind it to a listview in our html view We use the ViewData dictionary to pass data. We then loop through the array of data in our razor view and show the data in materialize list.

Questions this Examples helps answer.

  • How to pass array data from controller to view in core mvc.
  • ASP.NET MVC Core example.
  • ASP.NET Core MVC passing data via ViewData dictionary.
  • Passing data via ViewBag object to view.
  • ASP.NET MVC Core Controller and Action method examples.

We assume that you have visual studio and ASP.NET Core installed. We use of the built in templates and customize them. Watch the video tutorial below for more info.


  • Here's the screenshot of the project.

ASP.NET MVC Core Pass Data to List

Project Structure

  • Below is the project structure of our example.

ASP.NET MVC Core Pass Data to List

Tools Used

Language : C#, HTML,CSS Framework: ASP.NET Core MVC Platform : Backend and Fronted Web IDE : Visual Studio Topics : ViewBag ASP.NET Core,ViewData,Passing List ASP.NET MVC Core

Libraries Used

These are the third party CSS and JS used in this project.

  • MaterializeCSS.

Source Code

Lets have a look at the project's source code. [su_tabs] [su_tab title="HomeController.cs"]

  • Our Controller class.
  • Derives from he ASPNETCore.MVC.Controller base class.
  • Our public methods are action methods, routed to different seo friendly urls.
  • They return ActionResult objects. By convention, they are mapped to views with similar names.
  • Uses a ViewData dictionary or ViewBag to pass data from controller to our views.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;

namespace ListPass.Controllers
    public class HomeController : Controller
        public IActionResult Index()
            ViewData["description"] = "Our universe is amazing. It has over 100 billion galaxies. Each galaxy has over 100 billion stars. Just remarkable.";
            string[] spacecrafts = {"Whirlpool","Messier 81","Cosmos Redshift","NGC 6872","Andromeda","Milky Way","Sombrero","StarBust","Centarus A"};
            ViewData["spacecrafts"] = spacecrafts;
            return View();

        public IActionResult Error()
            return View();



  • Our master page/view.
  • It's shared and other public views shall derive from it.
  • It defines the header section,footer section and content section for the children. Then it calls the RenderBody() to provide the space for the children to show themselves.
  • We also show menu items here. Take note we have applied the materializecss them here.
<!DOCTYPE html>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>@ViewData["Title"] -</title>

    <environment names="Development">
        <link rel="stylesheet" href="~/lib/materialize/dist/css/materialize.min.css" />
        <link rel="stylesheet" href="~/css/style.css" />
    <environment names="Staging,Production">
        <link rel="stylesheet" href=""
              asp-fallback-test-class="sr-only" asp-fallback-test-property="position" asp-fallback-test-value="absolute" />
        <link rel="stylesheet" href="~/css/style.css" asp-append-version="true" />
    <nav class="light-blue lighten-1" role="navigation">
        <div class="nav-wrapper container">
            @Html.ActionLink("", "Index", "Home", new { area = "" }, new { @class = "brand-logo" })

            <ul class="right hide-on-med-and-down">
                <li>@Html.ActionLink("Home", "Index", "Home")</li>


            <ul id="nav-mobile" class="side-nav">
                <li>@Html.ActionLink("Home", "Index", "Home")</li>

            <a href="#" data-activates="nav-mobile" class="button-collapse"><i class="material-icons">menu</i></a>

    <div class="container">
        <hr />
    <footer class="page-footer orange">
        <div class="container">
            <div class="row">
                <div class="col l3 s12">
                    <h5 class="white-text">Company Bio</h5>
                    <p class="grey-text text-lighten-4">We are a team of college students working on this project like it's our full time job support and continue development on this project.</p>

                <div class="col l3 s12">
                    <h5 class="white-text">Languages</h5>
                        <li><a class="white-text" href="#!">C#</a></li>
                        <li><a class="white-text" href="#!">Java</a></li>
                        <li><a class="white-text" href="#!">PHP</a></li>
                        <li><a class="white-text" href="#!">Python</a></li>
                <div class="col l3 s12">
                    <h5 class="white-text">Categories</h5>
                        <li><a class="white-text" href="#!">Backend Web</a></li>
                        <li><a class="white-text" href="#!">Mobile</a></li>
                        <li><a class="white-text" href="#!">Desktop</a></li>
                        <li><a class="white-text" href="#!">Frontend Web</a></li>


    <environment names="Development">
        <script src="~/lib/jquery/dist/jquery.js"></script>
        <script src="~/lib/materialize/dist/js/materialize.min.js"></script>
        <script src="~/lib/materialize/dist/js/init.js"></script>

        <script src="~/js/site.js" asp-append-version="true"></script>
    <environment names="Staging,Production">
        <script src=""
        <script src=""
                asp-fallback-test="window.jQuery && window.jQuery.fn && window.jQuery.fn.modal">
        <script src="~/js/site.min.js" asp-append-version="true"></script>

    @RenderSection("scripts", required: false)



  • Our About Us page.
  • Is a razor view page. It can understand both html and c# code.
  • C# code is written in expressions beginning with "@" for inline code or "@{}" for block code.
  • Data is passed from the controller using the ViewData dictionary or ViewBag object. These are dynamic types and share the same dictionary for storage.
  • In this case case we pass an array of strings from the conrtoller to index.cshtml view.
    ViewData["Title"] = "Home Page";

<div class="section no-pad-bot" id="index-banner">
    <div class="container">
        <h1 class="header center orange-text">Amazing Universe</h1>
        <div class="row center">
            <h5 class="header col s12 light">@ViewData["description"]</h5>
        <div class="row">
            <ul class="collection with-header">
                <li class="collection-header"><h4>Galaxies</h4></li>
                @foreach (string s in ViewBag.Spacecrafts)
                    <li class="collection-item"><a href="#!">@s</a> </li>

            <a href="" id="download-button" class="btn-large waves-effect waves-light orange">Get Started</a>



Below is the video tutorial for this project.


Download the full project below: Download

How To Run

  1. Download the source code above.
  2. Extract it.
  3. In your Visual Studio: File -> Open ->Project/Solution.
  4. Choose the Solution location.
  5. Open
  6. That's it, you've imported the project to your visual studio.


  • Visit our channel for more examples like these.


How do You Feel after reading this?

According to scientists, we humans have 8 primary innate emotions: joy, acceptance, fear, surprise, sadness, disgust, anger, and anticipation. Feel free to tell us how you feel about this article using these emotes or via the comment section. This feedback helps us gauge our progress.

Help me Grow.

I set myself some growth ambitions I desire to achieve by this year's end regarding this website and my youtube channel. Am halfway. Help me reach them by:


What do You Think

Previous Post Next Post