title: How to retrieve value from ViewData when the object is not a string?
body: <p>Here's the functionality I'd like to exploit: I've a class <strong>myClass</strong> and would like to iterate over a collection that contains all the properties of that class. I'd like to send the index of that collection along with the other data so that I can control the each sequence of the iteration. </p>
  
  <p>Here's simplified versions of a Action method and View (I'll use the same action-view for that functionality).</p>
  
  <p>1) Action</p>
  
  <pre><code>public ActionResult CreateHierarchy(int? index)
  {
  
    if(index &lt; PropertiesOfMyClass.Lenght)
    {
      //Other code omitted 
  
      ViewData["index"] = ((index == null) ? 1 : index++);
  
      Return View();  
    }
  }
  </code></pre>
  
  <p>2)View</p>
  
  <pre><code>&lt;% Using(Html.BeginForm()){%&gt;      
     //Other Code omitted
  
     &lt;% = Html.Hidden("Index", ViewData["index"])%&gt;
     &lt;input type = "submit" value = "Do someting"/&gt;
   &lt;%}%&gt;
  </code></pre>
  
  <p>I've also placed this at the bottom of the page so that I can check the value of the index, </p>
  
  <pre><code>&lt;% = ViewData["index"]%&gt;
  </code></pre>
  
  <p>Unfortunately, its not working. I'm getting only the number 1. I'm missing something? such as a cast for the Viewdata? Should I write something like this:</p>
  
  <pre><code>&lt;% = Html.Hidden("index", (int)ViewData["index"])%&gt;
  </code></pre>
  
  <p>It's not working either</p>
  
  <p>=======EDIT April 6th/08h37AM</p>
  
  <p>myClass's real name is Hierarchy, which contains several levels like this</p>
  
  <pre><code>public class Hierarchy
  {
  public int HierarchyID { get; set;}
  public string Level1 { get; set; }
  public string Level2 { get; set; }
          ----
  public string Level7 { get; set; }
  }
  </code></pre>
  
  <p>Once I've the above properties in a collection, I can iterate that collection from Level1 to Level7 by turn (as value for each Level can have numerous sources). The index is important for me as rely on it to move forward or backward.</p>
  
  <p>Also I provided both the logic (1) in the action and in the View(2), so that one can follow how the index is passed back and forth between the action and the View.   </p>
  
  <p>Thanks for helping</p>