Html.DropDownList和PagedList问题

时间:2011-10-17 23:34:14

标签: c# asp.net-mvc-3 drop-down-menu pagedlist

我有一个小问题。我正在写一个简单的rss阅读器。所以我有频道和项目。这是项目视图。所以我有一个SelectedChannel给用户可以选择他想看的那种频道(默认情况下他会看到所有频道的所有项目)

`public ViewResult Index(string sortOrder, string currentFilter, 
        int? page, int? SelectedChannel)
    {

        var channels = from ch in db.Channels
                       orderby ch.Title
                       select ch;
        ViewBag.SelectedChannel = new SelectList(channels, "ChannelID", "Title", SelectedChannel);
        int channelID = SelectedChannel.GetValueOrDefault();

        ViewBag.DateSortParm = sortOrder == "Date" ? "Date desc" : "Date";
        if (Request.HttpMethod == "GET")
        {
            //page = 1;
        }
        else
        {
            page = 1;
        }
        var items = from i in db.Items
                    select i;
        if (SelectedChannel.HasValue)
        {
             items = from i in db.Items
                     where i.ChannelID.Equals(channelID)
                     select i;
        }
       ...
        return View(items.ToPagedList(pageIndex, pageSize));
    }`

这是来自index.cshtml的代码

@using (Html.BeginForm()) { <p>Select Channel: @Html.DropDownList("SelectedChannel", "All") &nbsp; <input type="submit" value="Filter" /></p> }

问题是:当用户过滤1个频道时。很好。但不是页面的脚(可能有很多页面)。因此,当用户尝试转到下一页时,过滤器参数会丢失。我看到所有项目的第二页。但我想看看selectedChannel的第二页(因为它再次变为null)。希望你明白我的意思。谢谢你!

2 个答案:

答案 0 :(得分:0)

`<div> 
Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) 
of @Model.PageCount 
&nbsp; 
@if (Model.HasPreviousPage) 
{ 
    @Html.ActionLink("<<", "Index", new { page = 1, sortOrder = ViewBag.CurrentSort, currentFilter=ViewBag.CurrentFilter  }) 
    @Html.Raw("&nbsp;"); 
    @Html.ActionLink("< Prev", "Index", new { page = Model.PageNumber - 1, sortOrder = ViewBag.CurrentSort, currentFilter=ViewBag.CurrentFilter  }) 
} 
else 
{ 
    @:<< 
    @Html.Raw("&nbsp;"); 
    @:< Prev 
} 
&nbsp; 
@if (Model.HasNextPage) 
{ 
    @Html.ActionLink("Next >", "Index", new { page = Model.PageNumber + 1, sortOrder = ViewBag.CurrentSort, currentFilter=ViewBag.CurrentFilter  }) 
    @Html.Raw("&nbsp;"); 
    @Html.ActionLink(">>", "Index", new { page = Model.PageCount, sortOrder = ViewBag.CurrentSort, currentFilter=ViewBag.CurrentFilter  }) 
} 
else 
{ 
    @:Next > 
    @Html.Raw("&nbsp;") 
    @:>> 
} 

`

这是index.cshtml的结尾

答案 1 :(得分:0)

这就是我的工作方式,它的工作原理,希望有人工作

@using (Html.BeginForm()) { <p>Select Channel: @Html.DropDownList("SelectedChannel", "All") &nbsp; <input type="submit" value="Filter" /></p> }

@if (Model.HasNextPage) 
{ 
    @Html.ActionLink("Next >", "Index", new { page = Model.PageNumber + 1, sortOrder = ViewBag.CurrentSort, currentFilter=ViewBag.CurrentFilter ciudad = "param-channel" }, new { id = "mylinkNext"  }) 
    @Html.Raw("&nbsp;"); 
    @Html.ActionLink(">>", "Index", new { page = Model.PageCount, sortOrder = ViewBag.CurrentSort, currentFilter=ViewBag.CurrentFilter, ciudad = "param-channel" }, new { id = "mylinkLast"  }) 
} 


<script type="text/javascript">
    $(function () {
        $('#mylinkNext').click(function () {
            var channel = $("#SelectedChannel").val();
            this.href = this.href.replace("param-channel",encodeURIComponent(channel));
        });
    });
  $(function () {
        $('#mylinkLast').click(function () {
            var channel = $("#SelectedChannel").val();
            this.href = this.href.replace("param-channel",encodeURIComponent(channel));
        });
    });
    </script>