skip to Main Content

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 = 'A'; letter <= 'Z'; letter++)
    {
        <h1 id=@letter>@letter</h1>       

        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?

4

Answers


  1. Chosen as BEST ANSWER

    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 = '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()))
                {
                    //do something here
                }
            }
        }
    }
    

    }

    Thank you all.


  2. Try this code man

    @{
        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)
                {
                    // Do something with each item here
                    <p>@item</p>
                }
            }
        }
    }
    
    Login or Signup to reply.
  3. You can use reflection to enumerate the internal ViewDataDictionary of the ViewBag:

    @{
        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[s] != null)
            {           
                //do something here          
            }
        }
    }
    
    Login or Signup to reply.
  4. 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<char,YourClass>()
       //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<char,YourClass> entry in ViewBag.ClassDic)
       {
           <h1 [email protected]>@entry.Key</h1>      
           foreach (var item in entry.Value))
           {
               // do something here
           }
       }       
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search