循环遍历具有变量名称的 ViewBags。

huangapple go评论76阅读模式
英文:

Loop through Viewbags with a variable name

问题

在我的ASP.NET MVC视图中,我想要循环遍历Viewbag.AViewBag.Z,但我不确定如何实现。

这是我在视图中的代码:

@{
    for (char letter = 'A'; letter <= 'Z'; letter++)
    {
        <h1 id=@letter>@letter</h1>       

        if (ViewBag.@letter != null)
        {
            foreach (var item in ViewBag.@letter)
            {
                 // 在这里执行一些操作
            }
        }
    }
}

有没有办法在ViewBag名称中使用变量,比如@letter

英文:

In my ASP.NET MVC View, I want to loop through Viewbag.A to ViewBag.Z but I'm not sure how to do this.

This is what I have in my view:

@{
    for (char letter = &#39;A&#39;; letter &lt;= &#39;Z&#39;; letter++)
    {
        &lt;h1 id=@letter&gt;@letter&lt;/h1&gt;       

        if (ViewBag.@letter != null)
        {
            foreach (var item in ViewBag.@letter))
            {
                 // do something here
            }
        }
    }
}

Is there a way to use a variable such as @letter in ViewBag name?

答案1

得分: 1

你可以使用反射来枚举ViewBag的内部ViewDataDictionary

@{
    var dc = ViewBag.GetType()
          .GetProperty("ViewData",
              System.Reflection.BindingFlags.Public |
              System.Reflection.BindingFlags.NonPublic |
              System.Reflection.BindingFlags.Instance)
          .GetValue(ViewBag, null);

    for (char letter = 'A'; letter <= 'Z'; letter++)
    {
        var s = letter.ToString();
        <h1 id=@letter>@letter</h1>
        if (dc
展开收缩
!= null)
{ //在这里执行某些操作 } } }
英文:

You can use reflection to enumerate the internal ViewDataDictionary of the ViewBag:

@{
    var dc = ViewBag.GetType()
          .GetProperty(&quot;ViewData&quot;,
              System.Reflection.BindingFlags.Public |
              System.Reflection.BindingFlags.NonPublic |
              System.Reflection.BindingFlags.Instance)
          .GetValue(ViewBag, null);

    for (char letter = &#39;A&#39;; letter &lt;= &#39;Z&#39;; letter++)
    {
        var s = letter.ToString();
        &lt;h1 id=@letter&gt;@letter&lt;/h1&gt;
        if (dc
展开收缩
!= null)
{ //do something here } } }

答案2

得分: 1

我认为更好的方法是使用包含您的 ViewBag 项目的字典,然后通过迭代它们来获取结果,这样您的代码会看起来像这样:

public IActionResult SomeView()
{
   var dic = new Dictionary<char, YourClass>();
   // 在这里填充您的字典与您的类
   // 其中字符将是键,类将是值
   ViewBag.ClassDic = dic;
   return View();
}

然后您的 Razor 脚本将类似于这样:

@{
   foreach (KeyValuePair<char, YourClass> entry in ViewBag.ClassDic)
   {
       <h1 id=@entry.Key>@entry.Key</h1>      
       foreach (var item in entry.Value)
       {
           // 在这里执行一些操作
       }
   }       
}
英文:

I think better way to get your result would be to have a dictionary containing your ViewBag items and iterating through them that way, so your code would look something like this:

public IActionResult SomeView()
{
   var dic = new Dictionary&lt;char,YourClass&gt;()
   //Populate your dictionary with your classes here
   //Where the character will be the key and the class will be the value
   ViewBag.ClassDic = dic;
   return View();
}

And then your razor script will look something like this:

@{
   foreach (KeyValuePair&lt;char,YourClass&gt; entry in ViewBag.ClassDic)
   {
       &lt;h1 id=@entry.Key&gt;@entry.Key&lt;/h1&gt;      
       foreach (var item in entry.Value))
       {
           // do something here
       }
   }       
}

答案3

得分: 0

试试这段代码吧

@{
    for (char letter = 'A'; letter <= 'Z'; letter++)
    {
        var propertyName = letter.ToString();
        <h1 id="@propertyName">@propertyName</h1>
        if (ViewBag is IDictionary<string, object> viewBagDict && viewBagDict.ContainsKey(propertyName))
        {
            var items = viewBagDict[propertyName] as IEnumerable<dynamic>;
            foreach (var item in items)
            {
                // 对每个项目执行一些操作
                <p>@item</p>
            }
        }
    }
}
英文:

Try this code man

@{
    for (char letter = &#39;A&#39;; letter &lt;= &#39;Z&#39;; letter++)
    {
        var propertyName = letter.ToString();
        &lt;h1 id=&quot;@propertyName&quot;&gt;@propertyName&lt;/h1&gt;
        if (ViewBag is IDictionary&lt;string, object&gt; viewBagDict &amp;&amp; viewBagDict.ContainsKey(propertyName))
        {
            var items = viewBagDict[propertyName] as IEnumerable&lt;dynamic&gt;;
            foreach (var item in items)
            {
                // Do something with each item here
                &lt;p&gt;@item&lt;/p&gt;
            }
        }
    }
}

答案4

得分: -2

感谢David提供的线索。我将所有数值存储在一个单独的ViewBag中。然后在视图中,我使用StartsWith()来过滤结果,如下所示:

@{
    for (char letter = 'A'; letter <= 'Z'; letter++)
    {
        <h1 id=@letter>@letter</h1>

        if (ViewBag.F != null)
        {
            foreach (var item in ViewBag.F)
            {
                if (item.Title.StartsWith(@letter.ToString()))
                {
                    //在这里执行某些操作
                }
            }
        }
    }
}

谢谢大家。

英文:

Thank you David for the clue. I stored all the values in a single ViewBag. Then in the view, I used StartsWith() to filter the results as below

@{
for (char letter = &#39;A&#39;; letter &lt;= &#39;Z&#39;; letter++)
{
    &lt;h1 id=@letter&gt;@letter&lt;/h1&gt;


    if (ViewBag.F != null)
    {
        foreach (var item in ViewBag.F)
        {
            if (item.Title.StartsWith(@letter.ToString()))
            {
				//do something here
            }
        }
    }
}

}

Thank you all.

huangapple
  • 本文由 发表于 2023年6月26日 21:59:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76557388.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定