views:

57

answers:

2

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

=======EDIT April 6th/08h37AM

myClass's real name is Hierarchy, which contains several levels like this

public class Hierarchy
{
public int HierarchyID { get; set;}
public string Level1 { get; set; }
public string Level2 { get; set; }
        ----
public string Level7 { get; set; }
}

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.

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.

Thanks for helping

+1  A: 

Is the value that you initially pass into this Action method 1? If so, you'll consistently return 1 as you're using the ++ operation as a postfix operation. That is, the value will be incremented after the assignment to ViewData has taken place. If you perform a prefix operation your issue should be resolved, provided of course that my premise above is true.

For details about prefix v. postfix operations see the ++ Operator article on MSDN.

I should also point out that your boolean expression will always evaluate to false as Nullable<T> == null is always false. Instead you should consider modifying that expression to (or something similar):

ViewData["index"] = ((index.HasValue) ? index.Value + 1 : 1);
Richard C. McGuire
@MCGuire -- I like your expression. Now, how do I retrieve the index value so that I can inject it again back to the action Method? Is this correct? -- <% = Html.Hidden("index", ViewData["index"])%> -- Is this correct?? I wonder if (maybe) I need to add a cast in front of ViewData -- Like this -- (int)ViewData["index"]
Richard77
@Rich, good catch! +1
Lazarus
When I do this -- (int)ViewData["index"] -- as value for the hidden field, I get an exception -- Object not set to an instance of an object --. So if, every HTML tag stores its value as string, maybe I need some casting in my action???
Richard77
Your exception is likely caused by the ViewData not containing the key 'index', since your assignment to that key occurs within an if statement it is possible that initially it isn't set thus leading to that exception.
Richard C. McGuire
I was able to find out why I was get the exception. Now, I wonder if <% = Html.Hidden("Index", ViewData["index"])%>, maybe my action shouldn't have a -- int? index param, but rather a string index param?? How about that?
Richard77
A: 

After checking other forum, I was reminded that ViewData holds value so that it can display it back in case there's a problem. To get the functionality I want, I need to clear first ViewData, using the following statement:

ViewData.ModelState.Clear();
ViewData["index"] = index + 1; 

or

ModelState.Remove("index")
ViewData["index"] = index + 1;

The second statement it's better as it deals only with one entry (as opposed to clearing the whole ViewData dictionary). After that, I can re-assign a new value = index + 1 (if I use Clear()) or re-create a new entry called ViewData["index"] and assign the value index + 1 to it.

Thanks for all your answers.

Richard77