我有一个控制器,我想在_Layout.cshtml 的控制器中发送ViewBag数据。
例子:
_Layout.cshtml:
<!DOCTYPE html>
<html>
<head>
<title>@ViewBag.username Blog Site</title>
</head>
<body>
<h1>Welcome, @ViewBag.username</h1>
@RenderBody()
</body>
</html>
主控制器.cs
public class MainController : Controller
{
public ActionResult Index()
{
if (HttpContext.Session["user"] != null)
{
var user = (tbl_user)HttpContext.Session["user"];
ViewBag.username = "serife";
ViewBag.testdata = "hellooo!";
}
return View();
}
}
索引.cshtml:
<label>Index Page</label>
<br/>
<label>@ViewBag.testdata</label>
<br/>
<label>@ViewBag.username</label>
结果:
欢迎,
索引页
你好!
衬线