我想将一组字符串存储到Controller中的ViewBag中,以便在我的视图中使用。我希望能够做到这一点:
ViewBag.Autobots[0].FirstName = "Bumble"
ViewBag.Autobots[0].LastName = "Bee"
ViewBag.Autobots[1].FirstName = "Optimus"
ViewBag.Autobots[1].LastName = "Prime"
但是,为了做到这一点,我需要先声明Autobots数组。我尝试了几种不同的方法,但是在这个看似简单的事情上被绊倒了。会爱一些帮助!
答案 0 :(得分:10)
ViewBag.Autobots = new Autobots[2];
然后根据您的代码,您可以开始为其分配值:
ViewBag.Autobots[0].FirstName = "Bumble"
ViewBag.Autobots[0].LastName = "Bee"
ViewBag.Autobots[1].FirstName = "Optimus"
ViewBag.Autobots[1].LastName = "Prime"