2015-09-06 106 views
-1

我使用PagedList.Mvc,當我做過濾像 本地主機丟失:2241 /三星並轉到第2頁 的瀏覽器重定向到本地主機:/ 2241,而不是本地主機 :2241 /三星/第2頁PagedList.Mvc分頁時我過濾產品

我怎樣才能解決這個

ProductController.cs

public class ProductController : Controller 
{ 
    private readonly IProductRepository repository; 
    public ProductController(IProductRepository repo) 
    { 
     repository = repo; 
    } 
    public ViewResult List(int? page, string category) 
    { 
     ProductListViewModel model = new ProductListViewModel 
     { 
      Prodcuts = repository.Products 
       .Where(p => category == null || p.Category == category), 
      CurrentCategory = category 
     }; 
     return View (model.Prodcuts.ToList().ToPagedList(page ?? 1, 3)); 
    } 
} 

List.cshtml

@model IPagedList<OnlineShoppingStore.Domain.Entities.Product> 
@using PagedList; 
@using PagedList.Mvc; 
.... 
@foreach (var p in Model) 
{ 
    @Html.Partial("ProductSummary",p) 
} 
@Html.PagedListPager(Model, page => Url.Action("List", new {page })) 

ProductListViewModel.cs

public class ProductListViewModel 
{ 
    public IEnumerable <Product> Prodcuts { get; set; } 
    public string CurrentCategory { get; set; } 
} 

我該怎麼辦?

+0

我該怎麼做? –

+0

用完整的代碼編輯的帖子..我不是專業的,仍然在學習MVC所以請告訴我在哪裏我可以編輯或添加代碼..我做了你之前說'category = Model.CurrentCategory' ..和'@model IPagedList '但仍然無法正常工作..請寫一個代碼不解釋..我明白了..謝謝 –

回答

-1

ProductController.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Mvc; 
using OnlineShoppingStore.Domain.Abstract; 
using PagedList; 
using PagedList.Mvc; 
using OnlineShoppingStore.WebUI.Models; 



namespace OnlineShoppingStore.WebUI.Controllers 
{ 
public class ProductController : Controller 
{ 
    private readonly IProductRepository repository; 

    public ProductController(IProductRepository repo) 
    { 
     repository = repo; 
    } 
    public ViewResult List(int? page, string category, string Filter_Value) 
    { 

     if (category != null) 
     { 
      page = 1; 
     } 
     else 
     { 
      category = Filter_Value; 
     } 

     ViewBag.FilterValue = category; 

     ProductListViewModel model = new ProductListViewModel 
     { 
      Prodcuts = repository.Products 
       .Where(p => category == null || p.Category == category), 
      CurrentCategory = category 

     }; 


       return View (model.Prodcuts.ToList().ToPagedList(page ?? 1, 3)); 

      } 


     } 
} 

ProductListViewModel.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using OnlineShoppingStore.Domain.Entities; 

namespace OnlineShoppingStore.WebUI.Models 
{ 
public class ProductListViewModel 
{ 
    public IEnumerable <Product> Prodcuts { get; set; } 
    public string CurrentCategory { get; set; } 

} 
} 

List.cshtml

@model IPagedList<OnlineShoppingStore.Domain.Entities.Product> 
@using PagedList; 
@using PagedList.Mvc; 


@{ 
ViewBag.Title = "Products"; 
} 

@foreach (var p in Model) 
{ 
@Html.Partial("ProductSummary",p) 

} 

@{ 

} 
<div> 

@Html.PagedListPager(Model, page => Url.Action("List", new {page,  Filter_Value = ViewBag.FilterValue })) 


Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount