0

我有一个分页的扩展方法。目录中有 23 个条目,所以 3 页,

简而言之,我的控制器:

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 说明:最佳重载方法是 (decimal,decimal) 我有一些无效参数?

4

1 回答 1

1

试试这样:

Math.Max(1, page ?? 1) 

这样做的原因是因为该Max方法需要一个整数作为第二个参数,但您传递给它一个可为空的整数(您的page参数被声明为int? page)。通过使用空合并运算符 ( ??),如果此参数为空,则表示默认值。

于 2013-03-11T09:31:11.330 回答