Reference: Scott blog ASP. net mvc Framework (part 3): Passing viewdata from controllers to Views
In ASP. net mvc Framework, viewdata is transmitted from the Controller to the view through two methods: weak type and strong type. The usage of viewdata is briefly described and its advantages and disadvantages are analyzed.
1) weak type
Passed through viewdata dictionary, and the View File inherits from viewpage
Assign values to viewdata in controller action
Viewdata ["productlist"] =...
Viewdata ["categoryname"] =...
Renderview ("viewname ")
Display and convert viewdata ["productlist"] to the actual type when calling a view
Viewdata ["productlist"] As list <product>
Viewdata ["categoryname"] as string
Advantage: flexible data transmission
Disadvantage: explicit conversion is required in the view.
2) strong type
The View File is followed by viewpage <tviewdata>. tviewdata is the data transmitted from the Controller to views.
In controller-> action, you can pass the data through renderview ("viewname", tviewdata ).
Renderview can only pass one type of data. To pass more types of data, you must encapsulate the data to be passed into an object for transmission.
Advantage: no explicit data conversion is required.
Disadvantage: additional objects must be written.
Note: 1) only for the. aspx view will there be a difference between the so-called [weak type] and [strong type] data transmission. Other view engines will not distinguish this situation.
2) weak and strong types of data transmission can only use one transmission method at the same time, and cannot be used together.
renderview method in controller class:
protected void renderview (string viewname);
protected void renderview (string viewname, object viewdata);
protected void renderview (string viewname, string mastername);
protected virtual void renderview (string viewname, string mastername, object viewdata);