我想将一组字符串存储到Controller的ViewBag中以在视图中使用。我希望能够做到这一点:
ViewBag.Autobots[0].FirstName = "Bumble"
ViewBag.Autobots[0].LastName = "Bee"
ViewBag.Autobots[1].FirstName = "Optimus"
ViewBag.Autobots[1].LastName = "Prime"
但是,为此,我需要先声明Autobots数组。我尝试了几种不同的方法,但是被这看似简单的事情绊倒了。希望有帮助!
最佳答案
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"