Which method does MVC use to pass the Model? Choose between ViewData, ViewBag, PartialView, TempData, ViewModel, and Tuple. mvcpartialview
In "passing multiple models to a view using the MVC controller, using ViewData, ViewBag, partial views, TempData, ViewModel, and Tuple", you can use different methods to pass multiple models, viewData, ViewBag, PartialView, TempData, ViewModel, and Tuple can all be used to transmit models. How can we choose between them? This article mainly includes:
- Use ViewData
- Use ViewBag
- Use PartialView
- Use TempData
- Use ViewModel
- Use Tuple
- Summary
Use ViewData
ViewData is a property of the ViewDataDictionary type in ControllerBase. It is stored as a key-Value Pair and obtained through the key.
Main purpose:
ViewData and ViewBag play the same role, except in the early ASP. in net mvc versions (1 and 2), ViewData is used. ViewBag is introduced in later versions, which has more advantages than ViewData. ViewBag is recommended.
Advantages:
● Convenience: store the Model as a key-value pair, and then pass it from the Controller to the view.
Disadvantages:
● One-way transmission: it can only be transferred from the Controller to the view.
● Only models in the current request can be stored: Models in multiple requests cannot be maintained. Once the page jumps, the value of ViewData becomes null.
● "Small Data" only: ViewData is not recommended if you want to transmit "Big Data.
● The ViewBag is not as readable as the key acquisition method.
● No smart prompts
● No compilation error check
Use ViewBag
ViewData is a dynamic attribute in ControllerBase and a dictionary in the form of name/value. Dynamic type is a type introduced in C #4.0, which makes it unnecessary to specify the type during compilation.
Main purpose:
● Store the Model in the form of name/value, and then pass the Model from the Controller to the view. The view Title is set through ViewBag. Title.
Advantages:
● Convenience
● Using name to obtain the corresponding value is better than using the key of ViewData to obtain the corresponding value.
● You do not need to explicitly specify the type
Disadvantages:
● One-way transmission: it can only be transferred from the Controller to the view.
● Only models in the current request can be stored: Models in multiple requests cannot be maintained. Once the page jumps, the value of ViewBag becomes null.
● Only "Small Data" can be stored: if you want to pass "Big Data", ViewBag is not recommended.
● No smart prompts
● No compilation error check
Use PartialView
Main purpose:
Extracted view parts that need to be reused as part of the view.
Advantages:
● Convenient Reuse
● Only update a part of the main view
Disadvantages:
● Excessive use affects readability
Use TempData
In ControllerBase, TempData is an attribute of the TempDataDictionary type. It is a dictionary in the form of a key-value pair. The corresponding value is obtained through the key. The type must be explicitly specified when values are assigned. The difference between ViewData and ViewBag is that it can be used across controllers and actions, and the Session mechanism is used internally.
Main purpose:
It can be used to transmit some non-sensitive data, such as verification and error information.
Advantages:
● Cross-controller and cross-action
Disadvantages:
● Explicit value assignment is required, and whether the value is null must be determined to avoid errors.
● No smart prompts
Use ViewModel
It is a Model based on View requirements. It can merge multiple View models into a View Model in the form of attributes.
Advantages:
● Place multiple models in one View model
● Smart prompts
● Security: the Domain Model is hidden.
● When the Domain Model changes, you do not need to modify many views. You only need to modify the corresponding View Model.
● Loose coupling between Domain Model and View
Use Tuple
Tuple was launched in. NET 4.0 and is an ordered, immutable, fixed-size class that can store multiple types.
Main purpose:
It is suitable for transferring some "Small Data". If you do not want to create a View Model in ASP. net mvc, you can use Tuple instead.
Advantages:
● Provides a way to pass the Model without creating a View Model.
Disadvantages:
● Limited size: up to eight groups of data can be stored.
● The values are transmitted using item1, item2. .. it is difficult to determine which data group item1, item2. .. represents.
● Unsatisfactory smart prompts
Summary
● If "Small Data" is passed, we think of ViewBag and ViewData.
● If a View-based Model is used, we want to design a View Model for the View.
● If a part of a View needs to be reused, extract it into a Partial View.
● When cross-controller and cross-action transmission is required, we think of TempData.
● If you are passing "Small Data" and do not want to use the View Model, you can consider Tuple.
References:
How to Choose the Best Way to Pass Multiple Models in ASP. NET MVC
How does aspnet mvc configure multiple models in a View? This example uses PartialView to implement this secondary introduction. ViewModels is used to compile models.
In the past, I learned about the usage of simplified models. Therefore, the development process should involve the use of simplified models. In other words, the MVC official website does not directly display a View. two Model examples are used to import ASP for the first time. net mvc friends will be stuck here. Basically, we use ViewData to compile the previous article, saying that the scattered model should not be used to develop the test plan, so we will package the desired items. A good way to form ViewModels
In this case, a new item is named ViewModels and a new Class content is added.
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Web;
Namespace MvcApplication. ViewModels {public class demoshopViewModels {public IEnumerable <shop {get; set ;}
Public IEnumerable <demo {get; set ;}}}
Use ViewModels to package two models.
The usage in View is not the same.
<% @ Page Language = C # MasterPageFile = ~ /Views/Shared/Site. Master Inherits = System. Web. Mvc. ViewPage <MvcApplication. ViewModels. demoshopViewModels %
<Asp: Content ID = aboutContent ContentPlaceHolderID = MainContent runat = server
<% Foreach (var item in Model. shop) {// shop Models} %
<% Foreach (var item in Model. demo) {// here demo Models }%</asp: Content
After reading the Code, it should have been found that multiple Models are used. OK, the process may be developed, and you may want to use the linear model to compile the program (you need to enable VideModels for someone else) therefore, the ViewData method is described below.
To use ViewData, You need to implement the resources in the Controller.
Public ActionResult About (){
ViewData [shop] = store;
ViewData [demo] = demo;
// The preceding two examples should have their own calling methods in real time
Return View ();}
Because it is impossible to directly inherit the use of the View, some changes may occur.
<% @ Page Language = C # MasterPageFile = ~ /Views/Shared/Site. Master Inherits = System. Web. Mvc. ViewPage %
<Asp: Content ID = aboutContent ContentPlaceHolderID = MainContent runat = server
<% Var shop = ViewData [shop] as IEnumerable <shop; %
<% Var demo = ViewData [demo] as IEnumerable <demo; %
-- %
TempData in mvc10 often cannot store data. How do you solve this problem? Use Session in TempData. Check the loss of stored sessions.