5 控制器,视图传值 (.net 上手笔记 )

104 阅读1分钟

1 创建mvc 项目 。

2 新建控制器。设置变量。

First Controller (多熟悉使用工具的感觉 。)

添加session 
{
    builder.Services.AddSession();
}

app.UseSession();

//控制器。
            ViewBag.user1 = "z3";
            ViewData["user2"] = "l4";
            TempData["user3"] = "w5";
            HttpContext.Session.SetString("user4", "z6");
            object user5 = "t7";  //注意细节 。


3 视图代码 。

@*
    For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
*@
@{
    ViewData["Title"] = "index";
}
@model String;

<h1> this is First index</h1>

<h2>User1=@ViewBag.user1</h2>
<h2>User2=@ViewData["user2"]</h2>
<h2>User3=@TempData["user3"]</h2>
<h2>User4=@Context.Session.GetString("user4")</h2>
<h2>User5=@Model</h2>  注意使用细节。

总结: 熟悉常规基础操作。 .net 最小化原则 ,所以session 要引入。 熟悉controller view 之前传值的约定用法。