Background
public static string ToQueryString(NameValueCollection nvc)
{
StringBuilder sb = new StringBuilder();
foreach(var key in nvc.AllKeys)
{
string value = nvc[key];
//EscapeDataString is the uri encoding of special characters
sb.Append(key).Append("=")
.Append(Uri.EscapeDataString(value)).Append(" &");
}
return sb.ToString().Trim(' &');// Remove the last superfluous&
}
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);
}
Front end
<< /span>div class= "menu-list clearfloat am-sharetwo">
<ul class="yiji" id="oe_menu">
<li >
<a href="#" class="inactive">area<i>i >a>< /span>
<ul style="display: none">
<li ><a href="~/House/[email protected](MVCHelper.RemoveQueryString(Request.QueryString,"RegionId"))">Alla >li>< /span>
@foreach (var region in Model.regions)
{
<li><a href="~/House/[email protected](MVCHelper.UpdateQueryString(Request.QueryString,"RegionId",region.Id.ToString ()))">@region.Namea>li>
}
ul>
li>
<li >
<a href="#" class="inactive">Rent<i>i >a>< /span>
<ul style="display: none">
<li ><a href="~/House/[email protected](Html.Raw(MVCHelper.RemoveQueryString(Request.QueryString,"MonthRent")))"MonthRent")))" span>>All a>li>
<li ><a href="~/House/[email protected](MVCHelper.UpdateQueryString(Request.QueryString,"MonthRent","*-100"))"< /span>>a>li>
<li ><a href="~/House/[email protected](MVCHelper.UpdateQueryString(Request.QueryString,"MonthRent","100-200"))"< /span>>100-200 yuana>li>
<li ><a href="~/House/[email protected](MVCHelper.UpdateQueryString(Request.QueryString,"MonthRent","201-500"))"< /span>>201-500 yuana>li>
<li ><a href="~/House/[email protected](MVCHelper.UpdateQueryString(Request.QueryString,"MonthRent","501-800"))"< /span>>501-800 yuana>li>
<li ><a href="~/House/[email protected](MVCHelper.UpdateQueryString(Request.QueryString,"MonthRent","801-1000"))"< /span>>801-1000 yuana>li>
<li ><a href="~/House/[email protected](MVCHelper.UpdateQueryString(Request.QueryString,"MonthRent","1001-*"))"< /span>>1000 yuan and abovea>li>
ul>
li>
<li >
<a href="#" class="inactive">Sort<i>i >a>< /span>
<ul style="display: none">
<li ><a href="~/House/[email protected](MVCHelper.UpdateQueryString(Request.QueryString,"OrderByType","MonthRentAsc"))">Rent from low to higha>li>
<li ><a href="~/House/[email protected](MVCHelper.UpdateQueryString(Request.QueryString,"OrderByType","MonthRentDesc"))">Rent from high to lowa>li>
<li ><a href="~/House/[email protected](MVCHelper.UpdateQueryString(Request.QueryString,"OrderByType","AreaAsc"))">Area from small to largea>li>
<li ><a href="~/House/[email protected](MVCHelper.UpdateQueryString(Request.QueryString,"OrderByType","AreaDesc"))">Area from large to smalla>li>
ul>
li>
ul>
div>
public static string ToQueryString(NameValueCollection nvc)
{
StringBuilder sb = new StringBuilder();
foreach(var key in nvc.AllKeys)
{
string value = nvc[key];
//EscapeDataString is the uri encoding of special characters
sb.Append(key).Append("=")
.Append(Uri.EscapeDataString(value)).Append(" &");
}
return sb.ToString().Trim(' &');// Remove the last superfluous&
}
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">area<i>i >a>< /span>
<ul style="display: none">
<li ><a href="~/House/[email protected](MVCHelper.RemoveQueryString(Request.QueryString,"RegionId"))">Alla >li>< /span>
@foreach (var region in Model.regions)
{
<li><a href="~/House/[email protected](MVCHelper.UpdateQueryString(Request.QueryString,"RegionId",region.Id.ToString ()))">@region.Namea>li>
}
ul>
li>
<li >
<a href="#" class="inactive">Rent<i>i >a>< /span>
<ul style="display: none">
<li ><a href="~/House/[email protected](Html.Raw(MVCHelper.RemoveQueryString(Request.QueryString,"MonthRent")))"MonthRent")))" span>>All a>li>
<li ><a href="~/House/[email protected](MVCHelper.UpdateQueryString(Request.QueryString,"MonthRent","*-100"))"< /span>>a>li>
<li ><a href="~/House/[email protected](MVCHelper.UpdateQueryString(Request.QueryString,"MonthRent","100-200"))"< /span>>100-200 yuana>li>
<li ><a href="~/House/[email protected](MVCHelper.UpdateQueryString(Request.QueryString,"MonthRent","201-500"))"< /span>>201-500 yuana>li>
<li ><a href="~/House/[email protected](MVCHelper.UpdateQueryString(Request.QueryString,"MonthRent","501-800"))"< /span>>501-800 yuana>li>
<li ><a href="~/House/[email protected](MVCHelper.UpdateQueryString(Request.QueryString,"MonthRent","801-1000"))"< /span>>801-1000 yuana>li>
<li ><a href="~/House/[email protected](MVCHelper.UpdateQueryString(Request.QueryString,"MonthRent","1001-*"))"< /span>>1000 yuan and abovea>li>
ul>
li>
<li >
<a href="#" class="inactive">Sort<i>i >a>< /span>
<ul style="display: none">
<li ><a href="~/House/[email protected](MVCHelper.UpdateQueryString(Request.QueryString,"OrderByType","MonthRentAsc"))">Rent from low to higha>li>
<li ><a href="~/House/[email protected](MVCHelper.UpdateQueryString(Request.QueryString,"OrderByType","MonthRentDesc"))">Rent from high to lowa>li>
<li ><a href="~/House/[email protected](MVCHelper.UpdateQueryString(Request.QueryString,"OrderByType","AreaAsc"))">面积从小到大a>li>
<li><a href="~/House/[email protected](MVCHelper.UpdateQueryString(Request.QueryString,"OrderByType","AreaDesc"))">面积从大到小a>li>
ul>
li>
ul>
div>