ASP. NET MVC viewdata/viewbag Simple Summary

Source: Internet
Author: User

Recently encountered a problem in the project, is to use Viewbag.model to store anonymous objects to the view, but need to add attributes to the anonymous object according to the conditions, this can be really difficult, Google found a lot of solutions are dynamically compiled God horse, feel good tall on, and finally did not adopt, because do not know the performance of dynamic compilation of large consumption.

Finally, I have a simple study of the Viewbag.model, stored in the dictionary<string, Object>, in view through the Model[key] way can read the corresponding value, Here is a simple summary of the use of ViewData and ViewBag:
1, Viewdata.model can store ordinary objects, anonymous objects, or dictionary, etc.;
2, ViewData itself is a dictionary;
3, Viewdata.model store anonymous objects need to dynamically append properties, you can use dictionary;
4, Viewdata.model and viewdata["Model" are equivalent;
5, ViewBag is the ViewData of a dynamic packaging vest, that is viewbag.name and viewdata["Name"] the same.

Since ViewData and viewbag are values stored using the dynamic object, a summary of the dynamic is further extended:
1, if the dynamic variable is stored in a normal object, you can use the normal "variable name." Object property name method to take the property value;
2, if the dynamic variable is stored in the dictionary, you can use the "variable name [Key]" method to take value;
3, if the dynamic variable is stored in a known type of object, you can manipulate the corresponding type of object as a way to operate;
4. If an anonymous object is stored in a dynamic variable, it can only be evaluated by reflection.

The above views only represent my superficial understanding, welcome Daniel to shoot bricks.

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: info-contact@alibabacloud.com 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.