非常有名的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
如你所见,有相当多的参数。显然,我希望在返回索引页面时保留所有这些参数,因此我需要将它们添加到ActionLink
中。
现在,我已经厌倦了手动做这件事,这对于1来说是可以的,但是对于6来说就不是了。
ActionLink
问题:如何将当前URL的所有参数作为可选的 RouteValues
**.**返回到中
我一直在找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") %>
发布于 2010-09-24 19:21:16
这就是我最终修复它的方式,我相当自豪,因为它工作得很好,而且非常干燥。
在视图中调用:
<%: Html.ActionLinkwParams("Back to List", "Index")%>
但是对于重载,它可以是一个普通ActionLink需要的任何东西。
助手:
帮助器从url获取不在路由中的所有参数。例如:这个url:
http://localhost:50152/2011-2012/myController/Details/77?postalCode=9***&org=CLB
因此,它将获取postalCode和组织并将其放入新的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。我的代码在URL中接受所有这些参数,通过将其设置为string.Empty,我从列表中删除了这个参数。
欢迎对其进行优化的意见或想法。
发布于 2011-04-07 23:05:33
为Request.QueryString创建一个ToRouteValueDictionary()扩展方法,以按原样使用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?
这比公认的答案更简单,涉及的代码要少得多。
发布于 2012-03-25 10:46:26
下面是ViewContext的一个扩展方法,它根据请求路由值和查询字符串创建一个RouteValueDictionary。
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.Action
@Html.ActionLink("5", "Index", "Product",
ViewContext.GetCombinedRouteValues(new { Page = 5 }),
new Dictionary<string, object> { { "class", "page-link" } })
如果请求URL中不存在Page参数,则会将其添加到生成的URL中。如果它确实存在,它的值将被更改为5。
This article对我的解决方案有更详细的解释。
https://stackoverflow.com/questions/3779932
复制相似问题