ASP. net mvc Framework: Passing viewdata from controllers to Views

Source: Internet
Author: User
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);

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.