Asp.net mvc 前台获取后台页面传值的方法
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.前台代码
@using Mvc.Models; <h2>mvc传值</h2> <br /> <h4>第一种方式ViewData["xxx"]</h4> ViewData["name1"]:@ViewData["name1"] <hr /> <h4>第二种方式ViewBag.自定义名称</h4> ViewBag.name:@ViewBag.name <hr /> <h4>第三种方式TempData["name"]</h4> TempData["name"]:@TempData["name"] <hr /> <h4>第四种方式View(model)</h4> @{ var res = "名称:" + Model.Name + ":" + "年龄:" + Model.Age; @:变量res的值为: @res }View Code