0

I have an url that works for passing a List of strings

/Home/Index?Person%5B0%5D=Myname&Person%5B1%5D=Yourname

Unencoded it is

/Home/Index?Person[0]=Myname&Person[1]=Yourname

The Action Method is

public ActionResult(List<string> person)
 {
...
}

The Parameter List person will be correctly filled with the values Myname and Yourname.

I need to redirect to this url using RedirectToAction

I would usually do

RedirectToAction("Index","Home",new {Parameter1=value1})

But obviously I cant use Person%5B0%5D as a parameter name, because it has ivalid characters.

How can I create such a link or should I use a different URL - scheme?

Mathias F
  • 15,906
  • 22
  • 89
  • 159
  • Did you try decoded version `Person[0]`? – George G Sep 19 '14 at 08:35
  • possible duplicate of [ASP.NET MVC - Pass array object as a route value within Html.ActionLink(...)](http://stackoverflow.com/questions/717690/asp-net-mvc-pass-array-object-as-a-route-value-within-html-actionlink) – CodeCaster Sep 19 '14 at 08:37
  • @George Person[0] is also not a valid property name. The url works, but I cant create the link. – Mathias F Sep 19 '14 at 08:44
  • @CodeCaster This is a different question. The link has an array with a different url scheme. I might find some usefull information there, but I dont see an answer to my problem. – Mathias F Sep 19 '14 at 08:46
  • The answer is in there, it's "use a RouteValueDictionary". – CodeCaster Sep 19 '14 at 08:58
  • If its just strings you can always use `/Home/Index?Person=Myname&Person=Yourname` with the parameter being `string[] person` –  Sep 19 '14 at 09:01
  • @stephen I shure can, but the real url is more complex and I need the roules in the routing table to reflect the real urls. – Mathias F Sep 19 '14 at 09:05
  • @CodeCaster Using RouteValueDictionary works. Do you want to add it as an answer? Its hard to see on the other question. – Mathias F Sep 19 '14 at 09:17
  • @Malcolm feel free to self-answer with your solution. :) – CodeCaster Sep 19 '14 at 09:21
  • @CodeCaster Thnx, I just wait for some time – Mathias F Sep 19 '14 at 09:23
  • What if you call it like RedirectToAction("Index","Home",new {person=new List(){"person1", "person2"}) – Ventsyslav Raikov Sep 19 '14 at 12:15

1 Answers1

1

hi i worked on your query and finally got the result just check this code find weather it work with your query.

Controller code :

public ActionResult showString()
        {
            try
            {

                IEnumerable<string> persons = new[] { "myname", "urname" };
                var values = new RouteValueDictionary(
                persons
                    .Select((sampleper, index) => new {sampleper, index })
                    .ToDictionary(
                        key => string.Format("[{0}]", key.index),
                        value => (object)value.sampleper
                    )
            );
            return RedirectToAction("details", values);




            }
            catch (Exception)
            {

                throw;
            }


        }

and another action method is details with list as parameter

public ActionResult details(IEnumerable<string> persons)
        {
             ViewBag.person = persons;



            return View();
        }

it also works if you pass the link as

http://localhost:2266/Home/details?%5B0%5D=myname&%5B1%5D=urname

the view of details action method is

@{
    ViewBag.Title = "details";
}

<h2>details</h2>
<ul>
  @foreach (var i in ViewBag.person)
  { 

  <li>@i</li>


  }

</ul>
Aravind Goud
  • 120
  • 2
  • 17