How do you refer to an action that takes an array as a parameter (RedirectToAction and / or ActionLink)?

I have an action defined like this:

public ActionResult Foo(int[] bar) { ... } 

Url, how it will work, as expected:

 .../Controller/Foo?bar=1&bar=3&bar=5 

I have another action that does some work and then redirects to the Foo action above for some calculated bar values.

Is there an easy way to specify route values ​​using RedirectToAction or ActionLink so that the URL is generated as in the example above?

They do not work:

 return RedirectToAction("Foo", new { bar = new[] { 1, 3, 5 } }); return RedirectToAction("Foo", new[] { 1, 3, 5 }); <%= Html.ActionLink("Foo", "Foo", new { bar = new[] { 1, 3, 5 } }) %> <%= Html.ActionLink("Foo", "Foo", new[] { 1, 3, 5 }) %> 

However, for one element of the array, they work:

 return RedirectToAction("Foo", new { bar = 1 }); <%= Html.ActionLink("Foo", "Foo", new { bar = 1 }) %> 

When you configure the panel to an array, it redirects to the following:

 .../Controller/Foo?bar=System.Int32[] 

Finally, this is with ASP.NET MVC 2 RC.

Thanks.

+4
source share
3 answers

There are several ways to do this. If you want to keep it stateless, do not use TempData and create an action filter.

Something like that:

ActionFilter:

 public class BindArrayAttribute:ActionFilterAttribute { public override void OnActionExecuting(ActionExecutingContext filterContext) { var keys = filterContext.HttpContext.Request.QueryString.AllKeys.Where(p => p.StartsWith("id")); var idArray = new int[keys.Count()]; var counter = 0; foreach (var key in keys) { var id = filterContext.HttpContext.Request.QueryString[key]; idArray[counter] = int.Parse(id); counter++; } filterContext.ActionParameters["id"] = idArray; base.OnActionExecuting(filterContext); } } 

Controller:

  [HttpPost] public ActionResult Index(ItemModel model) { var dic = new RouteValueDictionary(); var counter = 0; foreach (var id in model.SelectedItemIds) { dic.Add("id" + counter, id); counter++; } return RedirectToAction("Display", dic); } [HttpGet] [BindArray] public ActionResult Display(int[] id = null) { return View(id); } 
+2
source

I am not sure how to do this using existing helpers. But you can write your own method for this.

Here I threw something together:

  public static string EnumerableActionLink(this HtmlHelper htmlHelper, string linkText, string controllerName, string actionName, IEnumerable enumerable, string variableName) { var builder = new StringBuilder(string.Format("/{0}/{1}?", controllerName, actionName)); foreach (var item in enumerable) builder.Append(string.Format("{0}={1}&", variableName, item)); return string.Format("<a href=\"{0}\">{1}</a>", builder, linkText); } 

Usage example:

 <%= Html.EnumerableActionLink("Foo", "Foo", "Foo", new[] { 1, 3, 5 }, "bar")%> 
+1
source
 <%= Html.ActionLink("Foo", "Foo", "Foo", new[] { 1, 3, 5 }.Aggregate(string.Empty, (a, x) => a += "bar=" + x + "&")) %> 
+1
source

Source: https://habr.com/ru/post/1304206/


All Articles