Data transfer

Three ways for the controller to pass values ​​to the view

ViewData: Save data, you cannot pass data across pages
eg: Declare in the controller, Viewdtata["name"]="Tom", call in the view: @Viewdtata["name"]. Run the program

ViewBag: Save data, you cannot pass data across pages
eg: declare in the controller, ViewBag.name="Tom", call in the view: @ViewBag.name. Run the program

ViewBag and ViewData cannot pass data across the Action() method.

TempData: save data, can transfer data across pages
eg: declare in the controller, TempData["name"]="Tom", call in the view: @TempData["name"]. Run the program
TempData: The data transferred across pages is one-time, and the data will be cleared after each call.

View pass value to controller

Request: The most primitive way to transfer values ​​from the view to the background
eg:
Insert picture description here
Insert picture description here

1. Briefly describe the difference between ViewData ViewBag and TempData.

ViewData is only valid in the operation method of the current controller, and its life cycle is the same as that of the view. ViewBag is a dynamic type, e is dynamically analyzed when the program is running, and no type conversion is required when reading data. TempData data is stored in the Session by default, and can only be transferred by one controller at most, and each element can only be accessed once at most, and it is automatically deleted after access.

2. How do ViewData and ViewBag pass Model data?

(1) Connect to the database to query and return Model object data.

(2) Save Model data or data collection directly in ViewData or ViewBag.

(3) Access the model collection data through @foreach loop on the page, for example @foreach(var item in ViewData'users'] as List<Chapter2 .Models.Users>) means that ViewData stores a collection of users whose keys are collections Stored in the model class Users object.

3. What is the most primitive way for the view to transfer data to the controller? Briefly describe how to transfer and receive data.

(1) Use the Request object.

(2) Write forms and form elements on the view, and use Requestl "form element name attribute value"] to get the value in the submission processing controller.

Guess you like

Origin blog.csdn.net/weixin_46085790/article/details/106315993