如何使用ViewBag存储字符串数组

发布于 2025-01-08 03:09:23 字数 317 浏览 0 评论 0原文

我想将一组字符串存储到控制器中的 ViewBag 中,以便在我的视图中使用。我希望能够做到这一点:

ViewBag.Autobots[0].FirstName = "Bumble"
ViewBag.Autobots[0].LastName = "Bee"
ViewBag.Autobots[1].FirstName = "Optimus"
ViewBag.Autobots[1].LastName = "Prime"

但是,为了做到这一点,我需要首先声明 Autobots 数组。我尝试了几种不同的方法,但被这个看似简单的事情绊倒了。希望得到一些帮助!

I want to store a set of strings into the ViewBag in my Controller for use in my view. I want to be able to do this:

ViewBag.Autobots[0].FirstName = "Bumble"
ViewBag.Autobots[0].LastName = "Bee"
ViewBag.Autobots[1].FirstName = "Optimus"
ViewBag.Autobots[1].LastName = "Prime"

However, in order to do this I need to first declare the Autobots array. I've tried a few different ways, but getting tripped up on this seemingly simple thing. Would love some help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

紫﹏色ふ单纯 2025-01-15 03:09:23
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"
ViewBag.Autobots = new Autobots[2];

then as per your code you can start to assign values to it:

ViewBag.Autobots[0].FirstName = "Bumble"
ViewBag.Autobots[0].LastName = "Bee"
ViewBag.Autobots[1].FirstName = "Optimus"
ViewBag.Autobots[1].LastName = "Prime"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文