• 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.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>
  • 相关阅读:
    zookeeper安装教程(zookeeper3.4.5为例)
    解决VMware虚拟机网络时长中断的问题
    Linux 服务器上快速配置阿里巴巴 OPSX NTP服务
    配置使用 NTP
    jenkins配置从节点
    微信裂变红包
    微信从业人员推荐阅读的100本经典图书
    微信红包限额提升方法
    微信朋友圈运营规则
    微信公众平台开发问答
  • 原文地址:https://www.cnblogs.com/wangyinlon/p/11679161.html
Copyright © 2020-2023  润新知