【问题标题】:mvc.net 4 view not showing ViewBag.Messagemvc.net 4 视图未显示 ViewBag.Message
【发布时间】:2016-03-10 08:02:25
【问题描述】:

我在 Visual Studio 2012 中创建了空的 mvc.net 4 应用程序。 我添加了一个“服务”控制器:

public class ServiceController : Controller
    {
        //
        // GET: /Service/s

        public ActionResult Index()
        {
            ViewBag.text = "EEEE";
            return View();
        }

    }

然后我右键单击 ActionResult,并添加了一个名为“Index”的视图,然后我输入了以下代码:

@{
    ViewBag.Title = "Index";
}
<h2>Index</h2>
<br>
<h2>@ViewBag.Message</h2>

当我去链接 "http://localhost:4376/Service/Index" 时,我得到的所有输出都是: "

Index

,并且没有显示“EEEE”文本。我做错了什么?

【问题讨论】:

    标签: c# asp.net-mvc view controller


    【解决方案1】:

    ViewBag.text 不是ViewBag.Message。将视图代码更改为

    <h2>@ViewBag.text</h2>
    

    或控制器代码

    ViewBag.Message = "EEEE";
    

    【讨论】:

    • 谢谢,我不敢相信我没有看到。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-21
    • 2015-10-19
    • 1970-01-01
    • 1970-01-01
    • 2013-05-09
    • 2023-04-08
    相关资源
    最近更新 更多