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.