Home  >  Article  >  Backend Development  >  Introducing how MVC in ASP.NET passes data from controller to view

Introducing how MVC in ASP.NET passes data from controller to view

巴扎黑
巴扎黑Original
2017-08-16 14:17:301686browse

This article mainly introduces the four ways ASP.NET MVC transfers data from the controller to the view. It has certain reference value. Interested friends can refer to 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; }
  }

2. In the controller Instantiate this class


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

Method 1: ViewData

Use ViewData for methods in the controller in the form of key-value pairs The class stores the above instantiated object, as follows:


ViewData["person"] = p;

Then obtains the value in ViewData in the view and converts the object, as follows:


@{
  var p = (Products)ViewData["person"];
}
<h1>@p.Id</h1>
<h2>@p.Name</h2>
<h3>@p.Price</h3>

Method 2: ViewBag

Use the method in the controller to store the above object in the form of ViewBag dynamic expression, as follows:


ViewBag._Product = p;

Modify the view as follows:


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

Method 3: Model

Place the controller in The method returns 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 it cannot be obtained in the Detail method to the TempData object, because TempData can only be passed once and will be automatically cleared by the system.

Output result

The above is the detailed content of Introducing how MVC in ASP.NET passes data from controller to view. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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