Storing an Anonymous Object in ViewBag

前端 未结 4 578
Happy的楠姐
Happy的楠姐 2020-12-15 06:14

This is probably a silly question, but I am trying to stuff an anonymous object in ViewBag like so:

ViewBag.Stuff = new { Name = \"Test\", Emai         


        
4条回答
  •  不知归路
    2020-12-15 06:24

    Essentially the issue is that anonymous types are generated as internal (see answer), making hard typed references to the object's property impossible from the View. This article provides a more detailed explanation:

    http://www.heartysoft.com/anonymous-types-c-sharp-4-dynamic

    It is possible to accomplish with the use of a Dynamic Anonymous wrapper class (@Dakill's answer), but gets ugly fast and should make a programmer question why he/she would do so.

提交回复
热议问题