.NET5_项目建立+传值

一、控制器HomeController.cs中index方法传值给index.cshtml

1、ViewBag

2、ViewData

3、TempData

4、Model

5、Session

HomeController.cs

        public IActionResult Index()
        {
            base.ViewBag.User1 = "张三";
            base.ViewData["User2"] = "李四";
            base.TempData["User3"] = "王五";
            object User4 = "赵六";

            if(HttpContext.Session.GetString("User5")==null)//Session需要配置 builder.Services.AddSession(); app.UseSession();
            {
                HttpContext.Session.SetString("User5", "田七");
            }
            return View(User4);
        }

index.cshtml

@model System.String;
<h1>
    @base.ViewBag.User1
</h1>
<h1>
    @base.ViewData["User2"]
</h1>
<h1>
    @base.TempData["User3"]
</h1>
<h1>
    @Model
</h1>
<h1>
    @Context.Session.GetString("User5");
</h1>

 

二、控制器HomeController.cs接收值

localhost:50895/Home/index1 ---默认值0

localhost:50895/Home/index1?i=123 --传值123

 

posted @ 2023-10-09 09:26  它的眼角开过光  阅读(11)  评论(0编辑  收藏  举报