我必须分页的列表实际上是一个目录中的条目。我无法从数据库中获取数据。我的控制器部分:
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
}

那么在这种情况下,我该如何处理分页呢?
谢谢
发布于 2013-03-07 19:16:37
您可以使用模块列表上的Enumerable类的Skip和Take扩展方法对列表进行分页。
下面是一个完整的控制台应用程序,演示了如何做到这一点:
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);
}
}
}我将在你的控制器操作方法中进行分页和排序,并将排序后的列表传递给视图,这将使你的视图代码保持不变(你的视图实际上不应该自己进行分页和排序)。
action方法中的代码如下所示:
List<ScormModuleInfo> modules = new List<ScormModuleInfo>();
var pagedModules = modules
.Skip((page - 1) * pageSize)
.Take(pageSize);https://stackoverflow.com/questions/15266694
复制相似问题