ASP.NET MVC - 传递数组对象中Html.ActionLink路由值(...) [英] ASP.NET MVC - Pass array object as a route value within Html.ActionLink(...)

查看:201
本文介绍了ASP.NET MVC - 传递数组对象中Html.ActionLink路由值(...)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个返回数组的方法(字符串[]),我想这个字符串数组传递到操作链接,这样它会创建一个类似的查询字符串:

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:

/Controller/Action?str=val1&str=val2&str=val3...etc

但是,当我经过新{海峡= GetStringArray()}我得到以下网址:

But when I pass new { str = GetStringArray() } I get the following url:

/Controller/Action?str=System.String%5B%5D

所以基本上它采取我的字符串[]和在其上运行的ToString()来获取值。

So basically it's taking my string[] and running .ToString() on it to get the value.

任何想法?谢谢!

推荐答案

尝试创建一个RouteValueDictionary持有你的价值观。你必须给每个条目一个不同的密钥。

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 ) %>

会给你喜欢

<a href='/Controller/Action?str=val0&str=val1&...'>Link</a>

修改:MVC2改变了ValueProvider接口,使我原来的答复过时。你应该使用一个模型字符串数组作为属性。

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; }
}

然后模型粘结剂将填充你的模型与您在URL中传递的值。

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];
}

这篇关于ASP.NET MVC - 传递数组对象中Html.ActionLink路由值(...)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆