Reputation: 686
I am passing a Tuple to my partial view from my actionresult. I dont need to create a class because it will hold less than half a dozen items and will only be used once in the app. I had never heard of a Tuple before and it seemed liked the best way. Problem is how do I retrieve my results when I dont know how big the Tuple will be?
My code returns the error: "foreach statement cannot operate on variables of type 'object' because 'object' does not contain a public definition for 'GetEnumerator'"
Many sites explain this error "if you want to use foreach on Tuple then you can inherit Tuple class and implement the child class with IEnumerable interface and provide definition for GetEnumerator method." to which I sit here and scratch my head as if I'm reading Urdu.
What would be the best way to refactor this to pass a List of List? Thanx in advance for your suggestions.
My Actionresult:
List<Tuple<string, string>> mylist = new List<Tuple<string, string>>();
string[] bits = stringvar.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
foreach (string bit in bits)
{
... //some string manipulation here
mylist.Add(new Tuple<string, string>(string1, string2 ));
}
ViewData["FolderPath"] = mylist;
return PartialView("MyPartialView");
and my Partialview:
<% if (ViewData["FolderPath"] != null) {
var mystuff = ViewData["FolderPath"];
foreach (Tuple<string, string> item in mystuff)
{
string one = item.Item1;
string two = item.Item2;
... // do something with my strings
}
}
%>
Upvotes: 1
Views: 1356
Reputation: 61952
Where you write:
var mystuff = ViewData["FolderPath"];
you should try:
var mystuff = (List<Tuple<string, string>>)ViewData["FolderPath"];
Upvotes: 1