0

コントローラーがあり、_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>

MainController.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>

結果:

いらっしゃいませ、

インデックス
ページ
セリフ

4

0 に答える 0