ASP.Net Mvc应用程序中的分页问题

时间:2014-07-10 05:22:01

标签: asp.net-mvc

我正在开发一个MVC应用程序,我在其中使用DropdownList来选择" vendor"。当我从下拉列表中选择供应商时,该视图会显示与所选供应商相关的产品。

我使用分页来显示特定供应商产品的多个页面。

我的问题是当我从下拉列表中选择供应商时,在更改活动时,它会在所有页面上正确显示产品。如果我选择第二页,它会显示第二页的产品。但下次如果我从下拉列表中选择其他供应商,它会显示所选供应商的第二页。但我想要的是最初显示所选供应商的第一页。

控制器代码如下

public ActionResult Index(int? page ,int VendorId = 0)
        {
            var pageNumber = (page ?? 1);

            var pagesize = 2; 
            if (VendorId == 0)
            {
                VendorId = Convert.ToInt32(Session["InventoryVendorId"]);

            }
            VendorService vendorService = new VendorService();
            SelectList SelectList = new SelectList(vendorService.GetAll().OrderBy(t => t.Name), "Id", "Name", VendorId);
            ViewData["list"] = SelectList;

            int id = Convert.ToInt32(Session["loggedEmpId"]);
            CommonService.SetEmployeeId(id); 
            if (VendorId != 0)
            {
                Session["InventoryVendorId"] = VendorId; 
                ProductService ProductService = new ProductService();       
                var productList = ProductService.GetProductInventory().Where(x=>x.VendorId == VendorId);
                return View(productList.ToPagedList(pageNumber, pagesize));
            }
            else
            {
                return View();
            }

        }

视图代码如下

@model PagedList.IPagedList<StockWatch.DTO.ProductDTO>
@using PagedList.Mvc;
@using System.Web.UI.WebControls

@{
    ViewBag.Title = "Index";
 int VendorId = Convert.ToInt32(Session["InventoryVendorId"]);
}

<link href="~/Content/PagedList.css" rel="stylesheet" />
<div class="row-fluid">      
              <div  id="vendorDropdownDiv4" class="span12 " style="margin-left:0px;margin-top:10px;">    
                   <div class="span6"  >
                           <div class="span4" style="margin-left:1px;" >
                         <label >Vendor</label>
                    </div>
                    <div class="span6" >

                      @Html.DropDownList("VendorId", ViewData["list"] as SelectList, "-- Select vendor --", new { @id = "vendorIdforInventory", @name = "VendorId" })

                  </div>

                 </div>
                 <div class="span3" style="text-align:right">           
                @* <input class="btn btn-primary" type="submit" value="Load" id="create"/>*@
                 @*<input class="btn btn-default" value="Cancel" style="width:45px;" onclick="window.location.href='@Url.Action("index")    '"/>*@
           </div>
         </div> 
              </div> 


<div id="Newindexview"></div>
@if(Model != null)
{


</div>
     <div class="span12" style="margin-left:0px;">  
        <table>
            <thead>
                <tr >
                    <th style="width:250px;" >Product Name 
                    </th>

                    <th style="width:180px; text-align:left;" >Product Code
                    </th>
                    <th  style="border-right: solid #e8eef4 thick; width: 0px; text-align:right;">Avg. Weight
                    </th>

                    @{
                        foreach (var location in ViewBag.loc)
                        {
                        <th style="width:10px;text-align:right;">@location.Name</th>

                        }                     
                    }
                </tr>
            </thead>

            <tbody>               

                    @foreach (var p in Model)                    
                    {  
                      <tr>
                        <td style="width:250px;">

                            @p.Name
                        </td>
                          <td style="width:180px;text-align:left;">
                            @p.ProductCode
                          </td>
                           <td style="border-right: solid #e8eef4 thick; width: 15px; text-align:right">
                            @p.AvgWeight
                          </td>
                           @foreach (var location in ViewBag.loc)
                              {
                               flag = false;  
                               if(p.Inventory != null)
                               {
                                   foreach (var loc in p.Inventory)
                                   {

                                     if (location.Name == loc.LocationName)
                                     {
                                          <td style="width:10px; text-align:right;">@loc.Quantity</td>  
                                         flag = true;                              
                                     }                                   
                                   } 
                               }
                                 if (flag == false)
                                 {
                                   <td style="width:10px; text-align:right;">0</td>    
                                 }                   
                          }                       
                         </tr>   
                    }
            </tbody>

        </table>
        </div>
    <div class="span12" style="margin-left:0px;">
       <div class="span6" style="margin-left:0px;">  

         @Html.PagedListPager(Model, page => Url.Action("Index", new {page ,searchContent=searchcontent}))

           </div> 
             </div>            
</div>
}

和jquery代码如下

$("#vendorIdforInventory").change(function () {

            var vendorid = $('#vendorIdforInventory').val();         
            $.ajax({
                url: '@Url.Action("Index", "Inventory")',
                data: {
                    VendorId: vendorid                

                },
                type: "POST",
                success: function (data) {                 
                    location.reload();
                    $('#modeldiv1').empty();
                    $('#vendorDropdownDiv4').hide();
                    $('#Newindexview').html("");
                    $('#Newindexview').html(data);

                }
            });          
        });

如何解决此分页问题?

1 个答案:

答案 0 :(得分:0)

您需要将视图中的currentFilter传递回控制器(使用VendorID),并相应地设置页面。详细信息位于http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/sorting-filtering-and-paging-with-the-entity-framework-in-an-asp-net-mvc-application分页的ASP.NET教程中。

看看他们是如何使用searchString和currentFilter变量实现过滤的,这将确保当输入新字符串(或从您的案例中的下拉列表中选择)时,寻呼响应相应。

具体来说,这是我认为您在代码中遗漏的内容

if (searchString != null)
{
   page = 1;
}
else
{
   searchString = currentFilter;
}

...


int pageNumber = (page ?? 1);