0

我必须分页的列表实际上是目录中的条目。我没有从数据库中获取数据。我的控制器部分:

public ActionResult Index()
{
  // Iterate over all folders in the SCORM folder to find training material

  UrlHelper urlHelper = new UrlHelper(HttpContext.Request.RequestContext);
  string scormRelativePath = urlHelper.Content("~/ScormPackages");
  string scormRootDir = HttpContext.Server.MapPath(scormRelativePath);
  string scormSchemaVersion = string.Empty;
  string scormTitle = string.Empty;
  string scormDirectory = string.Empty;
  string scormEntryPointRef = string.Empty;
  string scormIdentifierRef = string.Empty;
  Int16 scormLaunchHeight = 640;
  Int16 scormLaunchWidth = 990;
  bool scormLaunchResize = false;
  string scormRelativeHtmlPath = string.Empty;


  List<ScormModuleInfo> modules = new List<ScormModuleInfo>();

  foreach (var directory in Directory.EnumerateDirectories(scormRootDir))
  {
    ScormModuleInfo module = new ScormModuleInfo();
    //more code
  }  
 }

在我看来:

   <% int idx = 0;
   foreach (var module in Model)
   { %>
      //lists names of the folders in the ScormPackages directory
   }

在此处输入图像描述

那么在这种情况下我将如何处理分页呢?

谢谢

4

1 回答 1

1

您可以使用模块列表中类的SkipTake扩展方法对列表进行分页。Enumerable

下面是一个完整的控制台应用程序,演示了如何执行此操作:

class Program
{
    static void Main(string[] args)
    {
        IList<string> lst = new List<string>
        {
            "One",
            "Two",
            "Three",
            "Four",
            "Five",
            "Six",
            "Seven",
            "Eight",
            "Nine",
            "Ten",
        };

        int pageSize = 3;
        int page = 2;

        var pagedLst = lst
                       .Skip((page - 1) * pageSize)
                       .Take(pageSize);

        foreach (string item in pagedLst)
        {
            Console.WriteLine(item);
        }
    }
}

我会在您的控制器操作方法中进行分页和排序,并将排序列表传递给视图,这将使您的视图代码保持不变(您的视图不应该真正进行分页和排序本身)。

您的操作方法中的代码如下所示:

List<ScormModuleInfo> modules = new List<ScormModuleInfo>();

var pagedModules = modules
                   .Skip((page - 1) * pageSize)
                   .Take(pageSize);
于 2013-03-07T11:16:37.097 回答