1

我有一个小问题。我正在写一个简单的 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 的第二页(因为它再次变为空)。希望你能明白我的意思。感谢你!

4

2 回答 2

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 的结尾

于 2011-10-19T12:18:06.763 回答
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> 
于 2012-07-16T15:54:37.337 回答