为什么我的 ViewBag.CustomerName 为空?

问题描述 投票:0回答:0

尝试学习 ASP.NET Core 并尝试找出为什么 ViewBag.CustomerName == null。这是我在 HomeController 中设置它的方法:

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

然后我尝试在 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>

我已经检查了拼写,重新启动了程序并检查了 ViewImports.cshtml。我正在使用@using Test2.Controllers。我尝试将其缩短为@using Test2,但结果是相同的,CustomerName 在调试会话中返回为“null”。

null viewbag
© www.soinside.com 2019 - 2024. All rights reserved.