Prelude
1. 새 프로젝트의 Models 파일 아래에 새 Products 클래스를 만듭니다.
public class Products { public int Id { get; set; } public string Name { get; set; } public double Price { get; set; } }
2. 컨트롤러에서 이 클래스를 인스턴스화합니다. >
var p = new Products() { Id = 1, Name = "饮料", Price = 2.5 };
ViewData["person"] = p;
@{ var p = (Products)ViewData["person"]; } <h1>@p.Id</h1> <h2>@p.Name</h2> <h3>@p.Price</h3>
ViewBag._Product = p;
@{ var p = (Products)ViewBag._Product; }
public ActionResult Index() { var p = new Products() { Id = 1, Name = "饮料", Price = 2.5 }; return View(p); }
@using MvcTest.Models; @model Products @{ ViewBag.Title = "Index"; } <h1>@Model.Id</h1> <h2>@Model.Name</h2> <h3>@Model.Price</h3>
public ActionResult Index() { var p = new Products() { Id = 1, Name = "饮料", Price = 2.5 }; TempData["_product"] = p; return RedirectToAction("Order"); } public ActionResult Order() { return View(); }
@{ Products p = (Products)TempData["_product"]; }
public ActionResult Index() { var p = new Products() { Id = 1, Name = "饮料", Price = 2.5 }; TempData["_product"] = p; return RedirectToAction("Order"); } public ActionResult Order() { return RedirectToAction("Detail"); } public ActionResult Detail() { Products _product = (Products)TempData["_product"]; return View(""); }