2

I have the following data-

  IList<int> mylist= new List<int>();
    int value=0;
    ViewData["URL"] = "/Services/Getdata/?value="+&value+"&mylist="+mylist;

   // It hits this function   
    public void Getdata(int value,IList<int> mylist)
    {}

Now the problem is the list is not being passed over correctly...I am doing a GET here and cannot post the data...so now I know it has to some how pass it like &mylist[0]=2&mylist[1]=3..so on...So how can achieve this...I found this but are there other alternatives?

Please help me out here..I can't really use TempData in my case..all I want is to pass an list or array through wire in asp.net mvc? Has anyone done something like this before?

Community
  • 1
  • 1
Vishal
  • 12,133
  • 17
  • 82
  • 128

1 Answers1

2

You could use the following function to serialize your list to one string:

public string Serialize(System.Collections.Generic.IList<int> list, string paramName) {
    return String.Join("&", list.Select((value, index) => String.Format(CultureInfo.InvariantCulture, "{0}[{1}]={2}", paramName, index, value)));
}
marcind
  • 52,944
  • 13
  • 125
  • 111
  • a slight problem i have is I am trying to pass two lists...and somehow it does not do it right when i am doing it...it passes the same list in the both the lists (the second one)... – Vishal Nov 05 '10 at 15:54