MVC的ActionLink添加所有(可选)参数从当前的URL [英] MVC ActionLink add all (optional) parameters from current url

查看:251
本文介绍了MVC的ActionLink添加所有(可选)参数从当前的URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

本非常著名的 ActionLink的

 <%:Html.ActionLink(返回目录,索引)%GT;

现在,这个环节是我的详细信息视图。索引视图是一个搜索页面。那该网址如下:

<$p$p><$c$c>http://localhost:50152/2011-2012/Instelling/Details/76?gemeente=Dendermonde&post$c$c=92**&gebruikers$c$c=VVKSO114421&dossiernr=114421%20&organisatie=CLB

如你所见,参数相当的数量。很显然,我希望将所有这些参数,当我回到索引页,所以我需要将它们添加在 ActionLink的

现在,我累了手动这样做的,它的确定1,而不是6。这应该容易得多。

问:我如何返回当前URL的所有参数到 ActionLink的可选 RouteValues​​

我一直在寻找的Request.QueryString 。它要的东西这一点。我想在的Global.asax 写一些静态方法做的工作,但没有运气还的。也许有一个简单的方法来做到这一点,我不知道?

编辑:这是我想出了(工作)

Global.asax的:

 公共静态RouteValueDictionary optionalParamters(NameValueCollection中C){
        RouteValueDictionary R =新RouteValueDictionary();
        的foreach(在c.AllKeys字符串s){
            r.Add(S,C [S]);
        }
        返回ř;
    }

Details.aspx:

 &LT;%:Html.ActionLink(返回目录,索引,MVC2_NASTEST.MvcApplication.optionalParamters(的Request.QueryString))%GT;

我在哪里最好把这个code?没有的Global.asax 我猜...

编辑2:

 使用系统;
使用System.Web.Mvc;命名空间MVC2_NASTEST.Helpers {
    公共静态类ActionLinkwParamsExtensions {
        公共静态MvcHtmlString CustomLink(此的HtmlHelper帮手,串linktext的){
            //这里u可以使用助手来获得查看上下文,然后routvalue字典
            VAR routevals = helper.ViewContext.RouteData.Values​​;
            //这里u可以做任何ü要与路由值
            返回null;
        }    }
}
&LT;%@导入命名空间=MVC2_NASTEST.Helpers%GT;
...
&LT;%:Html.ActionLinkwParams(「指数」)%GT;


解决方案

这是怎么我终于固定它,我觉得挺自豪,因为它的工作非常好,非常干燥。

在View召唤:

 &LT;%:Html.ActionLinkwParams(返回目录,索引)%GT;

但与重载它可以是任何东西哪一个正常的ActionLink的需要。

助手:

的辅助取从URL哪些不是在路由的所有参数。
例如:这个网址:

<?pre> 的http://本地主机:50152 / 2011-2012 / myController的/细节/ 77邮政code = 9 ***放大器;组织= CLB

因此​​,将采取邮政code和组织,并将其放置在新的ActionLink的。
与过载,额外的参数,可以添加,并从现有网址参数可以被删除。

 使用系统;
使用System.Web.Mvc;
使用System.Web.Routing;
使用System.Collections.Specialized;
使用System.Collections.Generic;命名空间MVC2_NASTEST.Helpers {
    公共静态类ActionLinkwParamsExtensions {
        公共静态MvcHtmlString ActionLinkwParams(此的HtmlHelper帮手,串linktext的,串动,串控制器,对象extraRVs,对象htmlAttributes){            NameValueCollection中C = helper.ViewContext.RequestContext.HttpContext.Request.QueryString;            RouteValueDictionary R =新RouteValueDictionary();
            的foreach(在c.AllKeys字符串s){
                r.Add(S,C [S]);
            }            RouteValueDictionary htmlAtts =新RouteValueDictionary(htmlAttributes);            RouteValueDictionary额外=新RouteValueDictionary(extraRVs);            RouteValueDictionary M =合并(R,另计);            返回System.Web.Mvc.Html.LinkExtensions.ActionLink(帮手,linktext的,动作,控制器,男,htmlAtts);
        }        公共静态MvcHtmlString ActionLinkwParams(此的HtmlHelper帮手,串linktext的,串动作){
            返回ActionLinkwParams(帮手,linktext的,动作,NULL,NULL,NULL);
        }        公共静态MvcHtmlString ActionLinkwParams(此的HtmlHelper帮手,串linktext的,串动,串控制器){
            返回ActionLinkwParams(帮手,linktext的,动作,控制器,NULL,NULL);
        }        公共静态MvcHtmlString ActionLinkwParams(此的HtmlHelper帮手,串linktext的,串动,对象extraRVs){
            返回ActionLinkwParams(帮手,linktext的,动作,空,extraRVs,NULL);
        }        公共静态MvcHtmlString ActionLinkwParams(此的HtmlHelper帮手,串linktext的,串动,串控制器,对象extraRVs){
            返回ActionLinkwParams(帮手,linktext的,动作,控制器,extraRVs,NULL);
        }        公共静态MvcHtmlString ActionLinkwParams(此的HtmlHelper帮手,串linktext的,串动,对象extraRVs,对象htmlAttributes){
            返回ActionLinkwParams(帮手,linktext的,动作,空,extraRVs,htmlAttributes);
        }
        静态RouteValueDictionary合并(这RouteValueDictionary原来,RouteValueDictionary @new){            //创建一个包含隐式和自动生成的值的新字典
            RouteValueDictionary合并=新RouteValueDictionary(原件);            的foreach(在@new变种F){
                如果(merged.ContainsKey(f.Key)){
                    合并[f.Key] = f.Value;
                }其他{
                    merged.Add(f.Key,f.Value);
                }
            }            返回合并;        }
    }}

在View使用重载:

 &LT;%:Html.ActionLinkwParams(返回目录,索引,myController的新{testValue =这是一个测试,邮政code =的String.Empty},{新类@ =测试})%GT;

在URL我有paramters邮政code。与一些价值。我的code通吃它们在URL中,将其设置为的String.Empty,我从列表中删除此参数。

意见或想法表示欢迎优化它。

The very famous ActionLink:

 <%: Html.ActionLink("Back to List", "Index")%>

Now, this link is in my Details view. The Index view is a search page. The URL of that looks like this:

http://localhost:50152/2011-2012/Instelling/Details/76?gemeente=Dendermonde&postcode=92**&gebruikerscode=VVKSO114421&dossiernr=114421%20&organisatie=CLB

As you can see, quite the amount of parameters. Obviously I want to keep all these parameters when I return to the Index page, so I need to add them in the ActionLink.

Now, I'm tired of doing that manually, it's ok for 1, but not for 6. This should go a lot easier.

Question: How do I return All parameters of the current URL into the ActionLink as optional RouteValues.

I've been looking to Request.QueryString. It has to be something with that. I was thinking of writing some static method in Global.asax doing the job but no luck yet. Maybe there is an easy way to do this which I don't know about?

Edit: This is what I came up with (which works)

In global.asax:

    public static RouteValueDictionary optionalParamters(NameValueCollection c) {
        RouteValueDictionary r = new RouteValueDictionary();
        foreach (string s in c.AllKeys) {
            r.Add(s, c[s]);
        }
        return r;
    }

Details.aspx:

    <%: Html.ActionLink("Back to List", "Index", MVC2_NASTEST.MvcApplication.optionalParamters(Request.QueryString))%>

Where do I best put this code? not in Global.asax I guess...

Edit 2:

using System;
using System.Web.Mvc;

namespace MVC2_NASTEST.Helpers {
    public static class ActionLinkwParamsExtensions {
        public static MvcHtmlString CustomLink(this HtmlHelper helper, string linktext) {
            //here u can use helper to get View context and then routvalue dictionary
            var routevals = helper.ViewContext.RouteData.Values;
            //here u can do whatever u want with route values
            return null;
        }

    }
}


<%@ Import Namespace="MVC2_NASTEST.Helpers" %>
...
<%: Html.ActionLinkwParams("Index") %>

解决方案

This is how I finally fixed it, and i'm rather proud because it's working very well and very DRY.

The call in the View:

    <%: Html.ActionLinkwParams("Back to List", "Index")%>

but with the overloads it can be anything which a normal ActionLink takes.

The Helper:

The helper takes all parameters from the url which are not in the route. For example: this url:

http://localhost:50152/2011-2012/myController/Details/77?postalCode=9***&org=CLB

So it will take the postalCode and the Org and place it in the new ActionLink. With the overload, additional parameters can be added, and parameters from the existing url can be removed.

using System;
using System.Web.Mvc;
using System.Web.Routing;
using System.Collections.Specialized;
using System.Collections.Generic;

namespace MVC2_NASTEST.Helpers {
    public static class ActionLinkwParamsExtensions {
        public static MvcHtmlString ActionLinkwParams(this HtmlHelper helper, string linktext, string action, string controller, object extraRVs, object htmlAttributes) {

            NameValueCollection c = helper.ViewContext.RequestContext.HttpContext.Request.QueryString;

            RouteValueDictionary r = new RouteValueDictionary();
            foreach (string s in c.AllKeys) {
                r.Add(s, c[s]);
            }

            RouteValueDictionary htmlAtts = new RouteValueDictionary(htmlAttributes);

            RouteValueDictionary extra = new RouteValueDictionary(extraRVs);

            RouteValueDictionary m = Merge(r, extra);

            return System.Web.Mvc.Html.LinkExtensions.ActionLink(helper, linktext, action, controller, m, htmlAtts);
        }

        public static MvcHtmlString ActionLinkwParams(this HtmlHelper helper, string linktext, string action) {
            return ActionLinkwParams(helper, linktext, action, null, null, null);
        }

        public static MvcHtmlString ActionLinkwParams(this HtmlHelper helper, string linktext, string action, string controller) {
            return ActionLinkwParams(helper, linktext, action, controller, null, null);
        }

        public static MvcHtmlString ActionLinkwParams(this HtmlHelper helper, string linktext, string action, object extraRVs) {
            return ActionLinkwParams(helper, linktext, action, null, extraRVs, null);
        }

        public static MvcHtmlString ActionLinkwParams(this HtmlHelper helper, string linktext, string action, string controller, object extraRVs) {
            return ActionLinkwParams(helper, linktext, action, controller, extraRVs, null);
        }

        public static MvcHtmlString ActionLinkwParams(this HtmlHelper helper, string linktext, string action, object extraRVs, object htmlAttributes) {
            return ActionLinkwParams(helper, linktext, action, null, extraRVs, htmlAttributes);
        }




        static RouteValueDictionary Merge(this RouteValueDictionary original, RouteValueDictionary @new) {

            // Create a new dictionary containing implicit and auto-generated values
            RouteValueDictionary merged = new RouteValueDictionary(original);

            foreach (var f in @new) {
                if (merged.ContainsKey(f.Key)) {
                    merged[f.Key] = f.Value;
                } else {
                    merged.Add(f.Key, f.Value);
                }
            }

            return merged;

        }
    }

}

In the View using overloads:

 <%: Html.ActionLinkwParams("Back to List", "Index","myController", new {testValue = "This is a test", postalCode=String.Empty}, new{ @class="test"})%>

in the URL I have the paramters postalCode with some value. my code takes All of them in the URL, by setting it to string.Empty, I remove this parameter from the list.

Comments or ideas welcome on optimizing it.

这篇关于MVC的ActionLink添加所有(可选)参数从当前的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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