How to retrieve value from ViewData when the object is not a string?
Posted
by Richard77
on Stack Overflow
See other posts from Stack Overflow
or by Richard77
Published on 2010-04-06T12:32:51Z
Indexed on
2010/04/06
12:53 UTC
Read the original article
Hit count: 356
asp.net-mvc
|viewdata
Hello,
Here's the functionality I'd like to exploit: I've a class myClass 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.
Here's simplified versions of a Action method and View (I'll use the same action-view for that functionality).
1) Action
public ActionResult CreateHierarchy(int? index)
{
if(index < PropertiesOfMyClass.Lenght)
{
//Other code omitted
ViewData["index"] = ((index == null) ? 1 : index++);
Return View();
}
}
2)View
<% Using(Html.BeginForm()){%>
//Other Code omitted
<% = Html.Hidden("Index", ViewData["index"])%>
<input type = "submit" value = "Do someting"/>
<%}%>
I've also placed this at the bottom of the page so that I can check the value of the index,
<% = ViewData["index"]%>
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:
<% = Html.Hidden("index", (int)ViewData["index"])%>
It's not working either
Thanks for helping
© Stack Overflow or respective owner