[英]Does not contain definition for ToPagedList
I keep getting this error when trying to implement PagedList in my Controller. 尝试在我的Controller中实现PagedList时,我不断收到此错误。 The exact error reads
确切的错误读取
'DataIntelligence.Models.Execution' does not contain a definition for 'ToPagedList' and no extension method 'ToPagedList' accepting a first argument of type 'DataIntelligence.Models.Execution' could be found (are you missing a using directive or an assembly reference?)`
'DataIntelligence.Models.Execution'不包含'ToPagedList'的定义,并且没有扩展方法'ToPagedList'可以找到接受类型'DataIntelligence.Models.Execution'的第一个参数(你是否缺少using指令或程序集引用?)`
and I have no idea why. 而且我不知道为什么。 I have looked at similar question but can't seem to find the answer I am looking for.
我看过类似的问题,但似乎找不到我要找的答案。 I was wondering if anybody could help.
我想知道是否有人可以提供帮助。
using DataIntelligence.Models;
using PagedList;
using PagedList.Mvc;
using System;
using System.Linq;
using System.Web.Mvc;
public ActionResult Execution(int? page, int id = 0)
{
var execution = db.Executions.Find(id);
if (execution == null)
{
return HttpNotFound();
}
ViewBag.ExecutionSeconds = (execution.End - execution.Start).ToString(@"dd\.hh\:mm\:ss\.fff");
int pageSize = Int32.Parse(System.Configuration.ConfigurationManager.AppSettings["DefaultPageSize"]);
int pageNumber = (page ?? 1);
return View(execution.ToPagedList(pageNumber, pageSize));
}
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;
namespace DataIntelligence.Models
{
[Table("Execution")]
public class Execution
{
public int Id { get; set; }
public Int16 PackageId { get; set; }
public DateTime Start { get; set; }
public DateTime End { get; set; }
public Boolean Successful { get; set; }
public virtual ICollection<Step> Steps { get; set; }
}
}
@model PagedList.IPagedList<DataIntelligence.Models.Execution>
@using PagedList.Mvc;
<link href="Content/PagedList.css" rel="stylesheet" type="text/css" />
@{
ViewBag.Title = "Execution Details";
}
<script language="javascript" type="text/javascript" src="jquery-1.8.3.js"> </script>
<script>
function goBack() {
window.history.back();
}
</script>
<div class="jumbotron">
<h2>Execution Details</h2>
</div>
<fieldset>
<legend>Execution Id - @Html.DisplayFor(model => model.Id)</legend>
<div class="row">
<div class="col-sm-3">
<div class="display-label">
@Html.DisplayNameFor(model => model.PackageId)
</div>
<div class="display-field">
<a href="@Url.Action("Package", new { id=Model.PackageId})"> @Html.DisplayFor(model => model.PackageId) </a>
</div>
</div>
<div class="col-sm-3">
<div class="display-label">
@Html.DisplayNameFor(model => model.Start)
</div>
<div class="display-field">
@Html.DisplayFor(model => model.Start)
</div>
</div>
<div class="col-sm-3">
<div class="display-label">
@Html.DisplayNameFor(model => model.End)
</div>
<div class="display-field">
@Html.DisplayFor(model => model.End)
</div>
</div>
<div class="col-sm-3">
<div class="display-label">
Execution Time
</div>
<div class="display-field">
@ViewBag.ExecutionSeconds
</div>
</div>
</div>
<div class="row">
<div class="col-sm-3">
<div class="display-label">
@Html.DisplayNameFor(model => model.Successful)
</div>
<div class="display-field">
@Html.DisplayFor(model => model.Successful)
</div>
</div>
</div>
<br />
</fieldset>
<table>
<tr>
<th>
Step ID
</th>
<th id="stepname">
Name
</th>
<th id="stepdate">
Date
</th>
</tr>
@foreach (var step in Model.Steps) {
<tr>
<td>
@Html.DisplayFor(modelItem => step.Id)
</td>
<td id="steps">
@Html.DisplayFor(modelItem => step.Name)
</td>
<td id="date">
@Html.DisplayFor(modelItem => step.Date)
</td>
</tr>
}
</table>
<button class="btn btn-link" onclick="goBack()" style="padding-right: 0px; padding-top: 0px; padding-bottom: 0px; padding-left: 0px;">Back</button>
Page @(Model.Steps.PageCount < Model.Steps.PageNumber ? 0 : Model.Steps.PageNumber) of @Model.Steps.PageCount
@Html.PagedListPager(Model.Steps, page => Url.Action("Execution", new { page, sortOrder = ViewBag.CurrentSort}))
The DbSet.Find
method returns a single instance of your entity, not an enumeration. DbSet.Find
方法返回实体的单个实例,而不是枚举。 So applying ToPagedList
to that does not make sense. 因此将
ToPagedList
应用于此是没有意义的。 If you did want to wrap it in a paged list, you could change your query to this: 如果您确实希望将其包装在分页列表中,则可以将查询更改为:
var executions = db.Executions.Where(e => e.Id == id);
This will give you an IEnumerable
result with only a single value. 这将为您提供仅具有单个值的
IEnumerable
结果。
After reading your comments, it seems that you actually want to paginate the Steps
property only. 阅读完您的评论后,您似乎确实只想对
Steps
属性进行分页。 To do that, you are better off creating a view model instead of passing the database entity directly to your view, note how I have changed the type of the Steps
property to be a paged list: 要做到这一点,最好创建视图模型而不是将数据库实体直接传递给视图,请注意我如何将
Steps
属性的类型更改为分页列表:
public class ExecutionViewModel
{
public int Id { get; set; }
public Int16 PackageId { get; set; }
public DateTime Start { get; set; }
public DateTime End { get; set; }
public Boolean Successful { get; set; }
public virtual PagedList.IPagedList<Step> Steps { get; set; }
}
No your controller becomes something like this: 没有你的控制器变成这样:
public ActionResult Execution(int? page, int id = 0)
{
var execution = db.Executions.Find(id);
if (execution == null)
{
return HttpNotFound();
}
ViewBag.ExecutionSeconds = (execution.End - execution.Start).ToString(@"dd\.hh\:mm\:ss\.fff");
int pageSize = Int32.Parse(System.Configuration.ConfigurationManager.AppSettings["DefaultPageSize"]);
int pageNumber = (page ?? 1);
var executionModel = new ExecutionViewModel
{
Id = execution.Id,
PackageId = execution.PackageId,
Start = execution.Start,
End = execution.End,
Successful = execution.Successful,
Steps = execution.Steps.ToPagedList(pageNumber, pageSize)
};
return View(executionModel);
}
And don't forget to change your view to take the new object: 并且不要忘记更改视图以获取新对象:
@model DataIntelligence.ViewModels.ExecutionViewModel
Add following through NuGet: 通过NuGet添加以下内容:
PM> Install-Package PagedList.MVC
In your controller, add the following: 在控制器中,添加以下内容:
using PagedList;
And in your view, add the following: 在您的视图中,添加以下内容:
@using PagedList.Mvc;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.