Home >Backend Development >C#.Net Tutorial >Four ways to pass data from controller to view in ASP.NET MVC
Prelude
1. Under the Models file in the new project, create a new Products class:
public class Products { public int Id { get; set; } public string Name { get; set; } public double Price { get; set; } }
2. Instantiate this class in the controller
var p = new Products() { Id = 1, Name = "饮料", Price = 2.5 };
Method 1: ViewData
Use the method in the controller to use ViewData to store the above instantiated object in the form of key-value pairs, as follows:
ViewData["person"] = p;
Then get the value in ViewData in the view and convert the object as follows:
@{ var p = (Products)ViewData["person"]; } <h1>@p.Id</h1> <h2>@p.Name</h2> <h3>@p.Price</h3>
Method 2: ViewBag
Place the controller The method in uses the ViewBag dynamic expression to store the above objects, as follows:
ViewBag._Product = p;
Modify the view, as follows:
@{ var p = (Products)ViewBag._Product; }
Method 3: Model
Return the method in the controller to the above object of View, as follows:
public ActionResult Index() { var p = new Products() { Id = 1, Name = "饮料", Price = 2.5 }; return View(p); }
And we get the mandatory type object Products in the view, as follows:
@using MvcTest.Models; @model Products @{ ViewBag.Title = "Index"; } <h1>@Model.Id</h1> <h2>@Model.Name</h2> <h3>@Model.Price</h3>
Method 4: TempData
TempData can continue to be used through redirection because its value is saved in Session. However, TempData can only be passed once and will be automatically cleared by the system afterwards.
Below I will demonstrate switching from Index action to Order action, and output the value stored in TempData in the view.
First create a new Action method in the control and name it the Order method. The code is as follows:
public ActionResult Index() { var p = new Products() { Id = 1, Name = "饮料", Price = 2.5 }; TempData["_product"] = p; return RedirectToAction("Order"); } public ActionResult Order() { return View(); }
Modify the view as follows:
@{ Products p = (Products)TempData["_product"]; }
Assume that the code in the controller is modified as follows:
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(""); }
Turn to: Index — Order — Detail, then in the Detail method, the TempData object cannot be obtained, because TempData can only After one transfer, it will be automatically cleared by the system.
Output results
The above is the entire content of this article. I hope it will be helpful to everyone's learning, and I also hope everyone will visit PHP Chinese website.
For more related articles on the four ways ASP.NET MVC transfers data from the controller to the view, please pay attention to the PHP Chinese website!