mvc html绑定url,c# – MVC ActionLink从当前URL添加所有(可选)参数

这是我最终如何修复它,我很自豪,因为它的工作非常好,非常干。

视图中的调用:

但是由于过载,它可以是任何正常的ActionLink所需要的。

助手:

助手从url中获取所有不在路由中的参数。

例如:这个网址:

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

所以它需要postalCode和组织,并将它放在新的ActionLink中。

通过过载,可以添加其他参数,并且可以删除现有网址中的参数。

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;

}

}

}

在使用重载的视图中:

在URL中我有一些具有某些值的参数postalCode。我的代码将所有这些都放在URL中,通过将其设置为string.Empty,我从列表中删除此参数。

评论或想法欢迎优化。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值