ASP.NET MVCにおけるQueryStringパッチ更新
16907 ワード
バックグラウンド
フロントエンド
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.Namea>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>