1024programmer Asp.Net .NET Core MVC basic page value transfer method📃

.NET Core MVC basic page value transfer method📃

.NET Core MVC basic page value transfer method📃

.NET Core MVC basic page value transfer method📃

Foreword

I have been too busy with work recently, so I would like to briefly update my basic knowledge of .NET. Most interviewers will ask about several ways to transfer values ​​​​in .NET pages, so let’s explain and implement them in detail!

The values ​​passed on the page are divided into two categories

  • Category 1: The controller passes values ​​to the view
  • Category 2: The view passes values ​​to the controller

This article mainly talks about the controller passing values ​​to the view

Controller->View

Weakly typed data

ViewData

ViewData is a dictionary object that can store key-value pairs in the controller and then read these values ​​in the view.

ViewData is a ViewDataDictionary object accessed via the string key

Controller:

public ActionResult Index()
 {
     ViewData["Message"] = "Hello, World!";
     return View();
 }
 

View:

@ViewData["Message"]

Rendering:

image

ViewBag

ViewBag is a dynamic property that can dynamically add properties in the controller and read these properties in the view.

Controller:

public IActionResult Index()
 {
     ViewBag.Message = "Hello, coders working overtime!";
     return View();
 }
 

View:

@ViewBag.Message

Rendering:

image

TempData

A mechanism for temporarily storing data between requests. It is similar to Session, but unlike Session, the data of TempData is only valid between the current request and the next request, and will be automatically cleared afterwards.

TempData Usage scenarios:

  • Useful for redirects where data is required for multiple requests.
  • Implemented through the TempData provider using cookies or session state.

Controller:

public ActionResult Index()
 {
     return View();
 }

 public ActionResult NextPage()
 {
      TempData["Message"] = "Do you get overtime pay? Just work overtime?";
     return RedirectToAction("Index");
 }
 

Index view:

@TempData["Message"]

Rendering:

Direct access to Index view

image

Access the NextPage method in the controller for redirection

image

strongly typed data

Model

Models are the most common way to pass data to views. Create a model object in the controller and assign data to the model’s properties. Then declare the model type in the view and use @Model to read the entity

Controller:

public class User
 {
     public string UserName { get; set; }
 }

 public IActionResult Index()
 {
     User u = new User();
     u.UserName = "Overtime worker";
     return View(u);
 }
 

View:

@model User

 

@Model.UserName

You can also pass collections

public IActionResult Index()
 {
     List list = new List()
     {
         new User() { UserName = "Worker No. 1" },
         new User() { UserName = "Worker No. 2" }
     };
     return View(list);
 }
 
@model List

 
@foreach (var m in Model) {

@m.UserName

}

Rendering:

image

Reference link

Views in ASP.NET Core MVC | Microsoft Learn

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/808578

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索