Why is my ViewBag.CustomerName coming up null?

18 views Asked by At

Trying to learn ASP.NET Core and am trying to figure out why ViewBag.CustomerName is == null. Here is how I set it in the HomeController:

public class HomeController : Controller
    {
        public IActionResult Index()
        {
            ViewBag.CustomerName = "Johnny B Good";
            return View();
     }

and then I try to display the name in the Index.cshtml

@{
    string message = "Welcome!";
    if (ViewBag.CustomerName != null)
    {
        message = "Welcome Back!";
    }

   }

    <div class="text-center">
    <h1 class="display-4">@message</h1>

    @if (ViewBag.CustomerName == null)
    {<p>CustomerName is null</p>}
    <p>Customer Name: @ViewBag.CustomerName</p>
    <p>2 + 2 + @(2+2)</p>
    </div>

I have checked the spelling, restarted the program and checked the ViewImports.cshtml. I am using the @using Test2.Controllers. I've tried shortening it to @using Test2 but the result is the same, with the CustomerName coming back as "null" in debugging sessions.

0

There are 0 answers