2013-03-08 23 views
0

我有一個擴展方法可以完成分頁。有23項在目錄中,以便3頁,使用for循環獲取首頁問題的分頁

我的控制器簡稱:

public ActionResult Index(int? page) 
{ 
    List<ScormModuleInfo> modules = new List<ScormModuleInfo>(); 

    string[] dirs = Directory.GetDirectories(scormRootDir); 

    int totalResults = dirs.Count(); 
    int pageSize = PageSizeSettings.ScormPackages; 
    int totalPages = Math.Max(Convert.ToInt32(Math.Ceiling((double)totalResults/pageSize)), 1); 
    if (page >= 1) 
    { 
    int startresult = ((Math.Max(1, **page**) - 1) * pageSize) + 1; 
    int endresult = Math.Min(startresult + (pageSize - 1), totalResults); 
    for (int i = startresult; i <= endresult; i++) 
    { 
     //more code 
    } 
    } 
視圖

<div class="actions-left"> 
    <%= Html.GlobalisedPageLinks(Amico.Web.Mvc.Extensions.Enums.PageLinksFormat.Empty, Model.CurrentPage, Model.PageSize, Model.Total, x => Url.Action("Index", "Scorm", new { area = "Admin", page = x }))%> 
</div> 

擴展方法:

public static string GlobalisedPageLinks(this HtmlHelper html, Amico.Web.Mvc.Extensions.Enums.PageLinksFormat format, int currentPage, int pageSize, int totalResults, Func<int, string> pageUrl) 
{ 
    int totalPages = Math.Max(Convert.ToInt32(Math.Ceiling((double)totalResults/pageSize)), 1); 

    int startresult = ((Math.Max(1, currentPage) - 1) * pageSize) + 1; 
    int endresult = Math.Min(startresult + (pageSize - 1), totalResults); 

    string pagesText = html.Resource(Resources.Global.PageLinks.PageLinksFormatPages, currentPage, totalPages); 
    string resultsText = html.Resource(Resources.Global.PageLinks.PageLinksFormatResults, startresult, endresult, totalResults); 
    string firstText = html.Resource(Resources.Global.PageLinks.First); 
    string previousText = html.Resource(Resources.Global.PageLinks.Previous); 
    string nextText = html.Resource(Resources.Global.PageLinks.Next); 
    string lastText = html.Resource(Resources.Global.PageLinks.Last); 

    return "<span class='page-links'>" + html.PageLinks(format, currentPage, pageSize, totalResults, pageUrl, 
    pagesText, resultsText, firstText, previousText, nextText, lastText) + "</span>"; 
} 

我在Math.Max(1,page)上爲startresult獲取一條紅線,指出:最佳重載方法是(十進制,十進制)我有一些inval ID參數?

回答

1

嘗試這樣的:

Math.Max(1, page ?? 1) 

這樣做的原因是因爲Max方法需要一個整數作爲第二個參數,但你傳遞一個空整型(您page參數被聲明爲int? page)。通過使用空合併運算符(??),如果此參數爲空,則表示缺省值。

+0

啊,非常感謝你會記住這個! – 2013-03-11 09:44:43