ASP.NET MVC - Pass array object as a route value within Html.ActionLink(…)

前端 未结 6 2125
别跟我提以往
别跟我提以往 2020-12-01 23:13

I have a method that returns an array (string[]) and I\'m trying to pass this array of strings into an Action Link so that it will create a query string similar to:

6条回答
  •  被撕碎了的回忆
    2020-12-01 23:59

    Try creating a RouteValueDictionary holding your values. You'll have to give each entry a different key.

    <%  var rv = new RouteValueDictionary();
        var strings = GetStringArray();
        for (int i = 0; i < strings.Length; ++i)
        {
            rv["str[" + i + "]"] = strings[i];
        }
     %>
    
    <%= Html.ActionLink( "Link", "Action", "Controller", rv, null ) %>
    

    will give you a link like

    Link
    

    EDIT: MVC2 changed the ValueProvider interface to make my original answer obsolete. You should use a model with an array of strings as a property.

    public class Model
    {
        public string Str[] { get; set; }
    }
    

    Then the model binder will populate your model with the values that you pass in the URL.

    public ActionResult Action( Model model )
    {
        var str0 = model.Str[0];
    }
    

提交回复
热议问题