Home > Backend Development > C#.Net Tutorial > Four ways to pass data from controller to view in ASP.NET MVC

Four ways to pass data from controller to view in ASP.NET MVC

高洛峰
Release: 2017-01-14 11:46:39
Original
1722 people have browsed it

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; }
 }
Copy after login

2. Instantiate this class in the controller

var p = new Products()
      {
        Id = 1,
        Name = "饮料",
        Price = 2.5
      };
Copy after login

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;
Copy after login

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>
Copy after login

Method 2: ViewBag

Place the controller The method in uses the ViewBag dynamic expression to store the above objects, as follows:

ViewBag._Product = p;
Copy after login

Modify the view, as follows:

@{
  var p = (Products)ViewBag._Product;
}
Copy after login

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);
 
    }
Copy after login

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>
Copy after login

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();
    }
Copy after login

Modify the view as follows:

@{
  Products p = (Products)TempData["_product"];
}
Copy after login

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("");
    }
Copy after login

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

ASP.NET MVC从控制器传递数据到视图的四种方式

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template