访问像数组一样的Viewbag? [英] Access a Viewbag like an Array?

查看:157
本文介绍了访问像数组一样的Viewbag?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设一个名为

的视图包

  ViewBag.Modes 

包含以下内容:

$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $
手动
完成

如何通过索引访问视图包一个数组?

例如Simple在索引0处,它看起来像这样

  ViewBag.Modes [0] 

我尝试了上述方法,但它不起作用...如何复制这与viewbag或有我可以使用的解决方法?

解决方案

感谢您的帖子,但很快在写这篇文章之后,我使用模型&列表如下所示,这也可以使用viewbag而不是model完成。

 列表< string> list = new List< string>(); 
foreach(Models.AppModeInfo blah in Model.theAppModes)
{
list.Add(blah.Name);
}

var AppModeText =;
switch(item.AppModeId)
{
案例1:
AppModeText = list [0];
休息;
案例2:
AppModeText = list [1];
休息;
案例3:
AppModeText = list [2];
休息;
案例4:
AppModeText = list [3];
休息;
案例5:
AppModeText = list [4];
休息;
}


Imagine a view bag called

 ViewBag.Modes

this contains the following:

Simple
Advanced
Manual
Complete

How can I access the viewbag by index like you would in an array?

e.g Simple is at index 0 then it would look like this

ViewBag.Modes[0]

I tried the above but it doesn't work so...How can I replicate this with viewbag or is there a workaround I can use?

解决方案

Thanks for the posts but shortly after writing this I came up with this solution using a model & list as below this could also be done using viewbag instead of model.

List<string> list = new List<string>();
            foreach (Models.AppModeInfo blah in Model.theAppModes)
            {
                list.Add(blah.Name);
            }

            var AppModeText = "";
            switch (item.AppModeId)
            {
                case 1:
                    AppModeText = list[0];
                    break;
                case 2:
                    AppModeText = list[1];
                    break;
                case 3:
                    AppModeText = list[2];
                    break;
                case 4:
                    AppModeText = list[3];
                    break;
                case 5:
                    AppModeText = list[4];
                    break;
            }

这篇关于访问像数组一样的Viewbag?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆