ASP.Net MVC RouteData 和数组 [英] ASP.Net MVC RouteData and arrays

查看:23
本文介绍了ASP.Net MVC RouteData 和数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有这样的操作:

public ActionResult DoStuff(List<string> stuff)
{
   ...
   ViewData["stuff"] = stuff;
   ...
   return View();
}

我可以通过以下网址点击它:

I can hit it with the following URL:

http://mymvcapp.com/controller/DoStuff?stuff=hello&stuff=world&stuff=foo&stuff=bar

但是在我的 ViewPage 中,我有这个代码:

But in my ViewPage, I have this code:

<%= Html.ActionLink("click here", "DoMoreStuff", "MoreStuffController", new { stuff = ViewData["stuff"] }, null) %>

不幸的是,MVC 不够聪明,无法识别该操作需要一个数组,并展开列表以形成正确的 url 路由.相反,它只是在对象上执行 .ToString() ,该对象仅在 List 的情况下列出数据类型.

Unfortunately, MVC is not smart enough to recognize that the action takes an array, and unrolls the list to form the proper url route. instead it just does a .ToString() on the object which just lists the data type in the case of a List.

当目标 Action 的参数之一是数组或列表时,有没有办法让 Html.ActionLink 生成正确的 URL?

Is there a way to get Html.ActionLink to generate a proper URL when one of the destination Action's parameters is an array or list?

-- 编辑--

正如 Josh 在下面指出的,ViewData["stuff"] 只是一个对象.我试图简化问题,但导致了一个无关的错误!我实际上使用的是专用的 ViewPage<T>所以我有一个紧密耦合的类型感知模型.ActionLink 实际上看起来像:

As Josh pointed out below, ViewData["stuff"] is just an object. I tried to simplify the problem but instead caused an unrelated bug! I'm actually using a dedicated ViewPage<T> so I have a tightly coupled type aware Model. The ActionLink actually looks like:

<%= Html.ActionLink("click here", "DoMoreStuff", "MoreStuffController", new { stuff = ViewData.Model.Stuff }, null) %>

ViewData.Model.Stuff 被输入为列表的地方

Where ViewData.Model.Stuff is typed as a List

推荐答案

我认为自定义 HtmlHelper 应该是合适的.

I'm thinking that a custom HtmlHelper would be in order.

 public static string ActionLinkWithList( this HtmlHelper helper, string text, string action, string controller, object routeData, object htmlAttributes )
 {
     var urlHelper = new UrlHelper( helper.ViewContext.RequestContext );


     string href = urlHelper.Action( action, controller );

     if (routeData != null)
     {
         RouteValueDictionary rv = new RouteValueDictionary( routeData );
         List<string> urlParameters = new List<string>();
         foreach (var key in rv.Keys)
         {
             object value = rv[key];
             if (value is IEnumerable && !(value is string))
             {
                 int i = 0;
                 foreach (object val in (IEnumerable)value)
                 {
                     urlParameters.Add( string.Format( "{0}[{2}]={1}", key, val, i ));
                     ++i;
                 }
             }
             else if (value != null)
             {
                 urlParameters.Add( string.Format( "{0}={1}", key, value ) );
             }
         }
         string paramString = string.Join( "&", urlParameters.ToArray() ); // ToArray not needed in 4.0
         if (!string.IsNullOrEmpty( paramString ))
         {
            href += "?" + paramString;
         }
     }

     TagBuilder builder = new TagBuilder( "a" );
     builder.Attributes.Add("href",href);
     builder.MergeAttributes( new RouteValueDictionary( htmlAttributes ) );
     builder.SetInnerText( text );
     return builder.ToString( TagRenderMode.Normal );
}

这篇关于ASP.Net MVC RouteData 和数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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