【发布时间】:2010-09-23 15:29:15
【问题描述】:
非常有名的ActionLink:
<%: Html.ActionLink("Back to List", "Index")%>
现在,此链接位于我的详细信息视图中。索引视图是一个搜索页面。它的 URL 如下所示:
http://localhost:50152/2011-2012/Instelling/Details/76?gemeente=Dendermonde&postcode=92**&gebruikerscode=VVKSO114421&dossiernr=114421%20&organisatie=CLB
如您所见,参数数量相当多。显然我想在返回Index页面时保留所有这些参数,所以我需要在ActionLink中添加它们。
现在,我已经厌倦了手动操作,1 可以,但 6 不行。这应该会容易得多。
问题:如何将当前网址的所有参数作为可选的RouteValues返回到ActionLink。
我一直在寻找Request.QueryString。它必须与那个有关。我正在考虑在Global.asax 中编写一些静态方法来完成这项工作,但还没有运气。也许有一种我不知道的简单方法可以做到这一点?
编辑:这是我想出的(可行)
在 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))%>
我最好把这段代码放在哪里?不在Global.asax 我猜...
编辑 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") %>
【问题讨论】:
标签: c# asp.net asp.net-mvc actionlink routevalues