ASP.NET MVC中QueryString拼接更新

后台

       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>

 

你可能感兴趣的:(ASP.NET MVC中QueryString拼接更新)