代码之家  ›  专栏  ›  技术社区  ›  Stefanvds

MVC ActionLink从当前url添加所有(可选)参数

  •  19
  • Stefanvds  · 技术社区  · 14 年前

    ActionLink :

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

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

    正如你所看到的,相当多的参数。显然,我希望在返回索引页时保留所有这些参数,因此需要将它们添加到 .

    现在,我厌倦了手动操作,1可以,但6不行。这应该容易得多。

    问题:如何将当前URL的所有参数返回到 动作链接 RouteValues .

    我一直在找 Request.QueryString . 一定是因为这个。我在考虑写一些静态的方法 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;
        }
    

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

    我最好把这个代码放在哪里?歪投球 我想。。。

    编辑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") %>
    
    5 回复  |  直到 9 年前
        1
  •  20
  •   Stefanvds    14 年前

    这就是我最终修复它的方式,我很自豪,因为它工作得很好,非常干燥。

    视图中的调用:

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

    但是有了重载,它可以是一个普通ActionLink需要的任何东西。

    例如:此url:

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

    所以它将把postalCode和Org放在新的ActionLink中。 通过重载,可以添加其他参数,也可以删除现有url中的参数。

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

    在使用重载的视图中:

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

    在URL中,我有一些值的参数postalCode。我的代码通过将其设置为字符串。空,我从列表中删除此参数。

        2
  •  10
  •   Community CDub    8 年前

    创建 可拓方法请求.QueryString使用Html.ActionLink文件按原样并简化视图标记:

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

    您的扩展方法可能如下所示:

    using System.Web.Routing;
    using System.Collections.Specialized;
    
    namespace MyProject.Extensions
    {
        public static class CollectionExtensions
        {
            public static RouteValueDictionary ToRouteValueDictionary(this NameValueCollection collection)
            {
                var routeValueDictionary = new RouteValueDictionary();
                foreach (var key in collection.AllKeys)
                {
                    routeValueDictionary.Add(key, collection[key]);
                }
                return routeValueDictionary;
            }
        }
    }
    

    How do I use an extension method in an ASP.NET MVC View?

    这比公认的答案更简单,所涉及的代码也少得多。

        3
  •  6
  •   Bassem    13 年前

    using System.Collections.Specialized;
    using System.ComponentModel;
    using System.Linq;
    using System.Web.Mvc;
    using System.Web.Routing;
    
    namespace MyMvcApplication.Utilities
    {
        public static class ViewContextExtensions
        {
            /// <summary>
            /// Builds a RouteValueDictionary that combines the request route values, the querystring parameters,
            /// and the passed newRouteValues. Values from newRouteValues override request route values and querystring
            /// parameters having the same key.
            /// </summary>
            public static RouteValueDictionary GetCombinedRouteValues(this ViewContext viewContext, object newRouteValues)
            {
                RouteValueDictionary combinedRouteValues = new RouteValueDictionary(viewContext.RouteData.Values);
    
                NameValueCollection queryString = viewContext.RequestContext.HttpContext.Request.QueryString;
                foreach (string key in queryString.AllKeys.Where(key => key != null))
                    combinedRouteValues[key] = queryString[key];
    
                if (newRouteValues != null)
                {
                    foreach (PropertyDescriptor descriptor in TypeDescriptor.GetProperties(newRouteValues))
                        combinedRouteValues[descriptor.Name] = descriptor.GetValue(newRouteValues);
                }
    
    
                return combinedRouteValues;
            }
        }
    }
    

    您可以将创建的RouteValueDictionary传递给Html.ActionLink文件或者Url.操作

    @Html.ActionLink("5", "Index", "Product",
        ViewContext.GetCombinedRouteValues(new { Page = 5 }),
        new Dictionary<string, object> { { "class", "page-link" } })
    

    This article 对我的解决方案有更详细的解释。

        4
  •  1
  •   Muhammad Adeel Zahid    14 年前
    public static class Helpers
        {
            public static MvcHtmlString CustomLink(this HtmlHelper helper,string LinkText, string actionName)
            {
                var rtvals = helper.ViewContext.RouteData.Values;
                var rtvals2 = helper.RouteCollection;
                RouteValueDictionary rv = new RouteValueDictionary();
                foreach (string param in helper.ViewContext.RequestContext.HttpContext.Request.QueryString.AllKeys) 
                {
                    rv.Add(param, helper.ViewContext.RequestContext.HttpContext.Request.QueryString[param]);
                }
                foreach (var k in helper.ViewContext.RouteData.Values) 
                {
                    rv.Add(k.Key, k.Value);
                }
                return helper.ActionLink(LinkText, actionName, rv);
            }
        }
    

    我已经测试了这个和它的工作。可选参数可以从查询字符串中获取 HTH公司

        5
  •  0
  •   Dr Rob Lang Matthew Abbott    11 年前

    您可以这样编写html帮助程序:

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