后台
public static string ToQueryString(NameValueCollection nvc) { StringBuilder sb = new StringBuilder(); foreach(var key in nvc.AllKeys) { string value = nvc[key]; //EscapeDataString就是对特殊字符进行uri编码 sb.Append(key).Append("=") .Append(Uri.EscapeDataString(value)).Append("&"); } return sb.ToString().Trim('&');//去掉最后一个多余的& } public static string RemoveQueryString(NameValueCollection nvc,string name) { NameValueCollection newNVC = new NameValueCollection(nvc); newNVC.Remove(name); return ToQueryString(newNVC); } public static string UpdateQueryString(NameValueCollection nvc, string name,string value) { NameValueCollection newNVC = new NameValueCollection(nvc); if(newNVC.AllKeys.Contains(name)) { newNVC[name] = value; } else { newNVC.Add(name, value); } return ToQueryString(newNVC); }
前端
<div class="menu-list clearfloat am-sharetwo"> <ul class="yiji" id="oe_menu"> <li> <a href="#" class="inactive">区域<i></i></a> <ul style="display: none"> <li><a href="~/House/Search?@(MVCHelper.RemoveQueryString(Request.QueryString,"RegionId"))">全部</a></li> @foreach (var region in Model.regions) { <li><a href="~/House/Search?@(MVCHelper.UpdateQueryString(Request.QueryString,"RegionId",region.Id.ToString()))">@region.Name</a></li> } </ul> </li> <li> <a href="#" class="inactive">租金<i></i></a> <ul style="display: none"> <li><a href="~/House/Search?@(Html.Raw(MVCHelper.RemoveQueryString(Request.QueryString,"MonthRent")))">全部</a></li> <li><a href="~/House/Search?@(MVCHelper.UpdateQueryString(Request.QueryString,"MonthRent","*-100"))">100元以下</a></li> <li><a href="~/House/Search?@(MVCHelper.UpdateQueryString(Request.QueryString,"MonthRent","100-200"))">100-200元</a></li> <li><a href="~/House/Search?@(MVCHelper.UpdateQueryString(Request.QueryString,"MonthRent","201-500"))">201-500元</a></li> <li><a href="~/House/Search?@(MVCHelper.UpdateQueryString(Request.QueryString,"MonthRent","501-800"))">501-800元</a></li> <li><a href="~/House/Search?@(MVCHelper.UpdateQueryString(Request.QueryString,"MonthRent","801-1000"))">801-1000元</a></li> <li><a href="~/House/Search?@(MVCHelper.UpdateQueryString(Request.QueryString,"MonthRent","1001-*"))">1000元以上</a></li> </ul> </li> <li> <a href="#" class="inactive">排序<i></i></a> <ul style="display: none"> <li><a href="~/House/Search?@(MVCHelper.UpdateQueryString(Request.QueryString,"OrderByType","MonthRentAsc"))">租金从低到高</a></li> <li><a href="~/House/Search?@(MVCHelper.UpdateQueryString(Request.QueryString,"OrderByType","MonthRentDesc"))">租金从高到低</a></li> <li><a href="~/House/Search?@(MVCHelper.UpdateQueryString(Request.QueryString,"OrderByType","AreaAsc"))">面积从小到大</a></li> <li><a href="~/House/Search?@(MVCHelper.UpdateQueryString(Request.QueryString,"OrderByType","AreaDesc"))">面积从大到小</a></li> </ul> </li> </ul> </div>