我必须分页的列表实际上是目录中的条目。我没有从数据库中获取数据。 我的控制器部分:
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
}
那么在这种情况下我将如何处理分页?
感谢
答案 0 :(得分:1)
您可以使用模块列表中Skip
类的Take
和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);