首页 > Windows开发 > 详细

C# Asp.net MVC 分页时传参场景 动态生成QueryString

时间:2015-04-27 23:43:48      阅读:340      评论:0      收藏:0      [点我收藏+]

1. 获得url

2.解析query string,将外部传入的匿名对象的key/value添加进去

3. 添加该页面特殊的key/value

4.合并url

var raw = HttpContext.Current.Request.Url.AbsoluteUri;
    string[] separateURL = raw.Split(‘?‘);

//if found "?" parse query string else return empty HttpValueCollection
    var queryString = separateURL.Length == 2 ? HttpUtility.ParseQueryString(separateURL[1]) : HttpUtility.ParseQueryString("");

    //used to add additional query string key/value (pass anonymous object)
    //e.g :
    //@Html.DisplayFor(x => x.Paging, new
    //        {
    //            AdditionalViewData = new {
    //            Model.OrdersFilter, Model.SearchKeyword, Model.PickupDateRange,
    //            Model.RequestDateRange, Model.DeliveryDateRange
    //        }

//pass in from outside
    if (ViewData["AdditionalViewData"] != null)
    {
        foreach (var str in ViewData["AdditionalViewData"].GetType().GetProperties())
        {
            queryString[str.Name] = (str.GetValue(ViewData["AdditionalViewData"], null) ?? string.Empty).ToString();
        }
    }

//one more specific query strings on this page
            @{
                 queryString[xxx] = yyy;
            }
////

            <a href="@string.Format("{0}?{1}", separateURL[0], queryString)">something friendly</a>


C# Asp.net MVC 分页时传参场景 动态生成QueryString

原文:http://blog.csdn.net/lan_liang/article/details/45318907

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!