2011-10-17 8 views
1

小さな問題があります。私はシンプルなRSSリーダーを書いています。だから私はチャンネルとアイテムを持っている。これはアイテムのビューです。私は、ユーザーへのSelectedChannelは、彼が(デフォルトでは原因は、彼はすべてのチャンネルからすべての項目が表示されます)を参照していたいチャンネルの種類を選択することができていHtml.DropDownListとPagedListの問題

`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)); 
    }` 

そして、ここで

@using (Html.BeginForm()) { <p>Select Channel: @Html.DropDownList("SelectedChannel", "All") &nbsp; <input type="submit" value="Filter" /></p> } index.cshtmlからのコードです

問題は次のとおりです。ユーザーが1つのチャンネルをフィルタリングするとき。それは良いです。しかし、ページには足がありません(ページがたくさんあります)。したがって、ユーザーが次のページに移動しようとすると、フィルターパラメーターが失われます。そして、私はすべてのアイテムの2番目のページを見る。しかし、私はselectedChannelの2ページ目を見たい(再びnullになるため)。私が何を意味するのか分かりますように。ありがとう!

+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

0

の終わりで、希望somedyはどのようにページャを構築している

@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>