1.后台传值:
1 public class DataController : Controller
2 {
3 // GET: Data
4 public ActionResult Index()
5 {
6 //1 ViewData传值
7 ViewData["name1"] = "张三";
8
9 //2 ViewBag.name
10 ViewBag.name = "李四";
11
12 //3.TempData["name"]
13 TempData["name"] = "王五";
14
15 //4 view传值直接
16 PersonViewModel model = new PersonViewModel() { Name="赵六", Age=18};
17 return View(model);
18 }
19 }
View Code
2.前台展示
1 @using _06Mvc.Models;
2
3 <h2>mvc传值</h2>
4 <br />
5 <h4>第一种方式ViewData["xxx"]</h4>
6 ViewData["name1"]:@ViewData["name1"]
7 <hr />
8 <h4>第二种方式ViewBag.自定义名称</h4>
9 ViewBag.name:@ViewBag.name
10 <hr />
11 <h4>第三种方式TempData["name"]</h4>
12 TempData["name"]:@TempData["name"]
13 <hr />
14 <h4>第四种方式View(model)</h4>
15 @{
16 var res = "名称:" + Model.Name + ":" + "年龄:" + Model.Age;
17 @:变量res的值为: @res
18 }
View Code
3.效果: