我应该如何使用 ToPagedList 从控制器传递我的查询
How should I pass my query from controller using ToPagedList
实际上我想在我的视图页面中集成分页,为此我正在从下面的代码中检索数据,我正在将这段代码从控制器传递给视图但是我遇到的任何问题都是
var model = (from sr in db.StudentRequests
join c in db.Classes
on sr.ClassId equals c.ClassId
select new { sr.StudentRequestId,c.ClassName,sr.CreatedOn,sr.Location,sr.PaymentMethod }).ToList().ToPagedList(page ?? 1, 1);
return View(model);
我遇到了
的问题
Type : InvalidOperationException
Message : The model item passed into the dictionary is of type 'PagedList.PagedList`1[<>f__AnonymousType3`5[System.Int32,System.String,System.Nullable`1[System.DateTime],System.String,System.String]]', but this dictionary requires a model item of type 'PagedList.IPagedList`1[Student_Tutor.Models.StudentRequest]'.
Source : System.Web.Mvc
我的视角是
@using PagedList;
@using PagedList.Mvc;
@model IPagedList<Student_Tutor.Models.StudentRequest>
@if (ViewBag.StudentRequest != null)
{
var StudentRequestId = (int)Model.First().StudentRequestId;// Here I am able to get the StudentRequestId
var StudentRequestTimecount = StudentRequestTime.Where(d => d.StudentRequestId == StudentRequestId).ToList();
var TutorStudentRequestcount = TutorStudentRequest.Where(d => d.StudentRequestId == StudentRequestId).ToList();
@Html.Displayfor(model => model.First().StudentRequestId)// here only text is displaying as StudentRequestId
@Html.Displayfor(Model => Model.First().CreatedOn)//here only text is diplaying as created on
}
请解释为什么我会收到此错误?
更新 1
var model = (from sr in db.StudentRequests
join c in db.Classes
on sr.ClassId equals c.ClassId
select new Studentvm{ StudentRequestId = sr.StudentRequestId,ClassName= c.ClassName,
CreatedOn =Convert.ToDateTime(sr.CreatedOn),Location= sr.Location,PaymentMethod= sr.PaymentMethod })
.ToList().ToPagedList(page ?? 1, 1);
return View(model);
但我收到错误
An exception of type 'System.NotSupportedException' occurred in Student_Tutor.dll but was not handled in user code
Additional information: LINQ to Entities does not recognize the method 'System.DateTime ToDateTime(System.Object)' method, and this method cannot be translated into a store expression.
这部分 LINQ 代码
select new { sr.StudentRequestId,c.ClassName,sr.CreatedOn,sr.Location,sr.PaymentMethod }
这是为您从 LINQ 查询获得的结果集合中的每个项目创建一个匿名对象,并且您正在从中创建一个 PagedList
。但是您的视图被强类型化为 PagedList<StudentRequest>
理想的解决方案是创建一个视图模型来表示此视图所需的数据,并将其用于 LINQ 查询的投影部分
public class StudentVm
{
public int StudentRequestId { set;get;}
public string ClassName { set;get;}
public DateTime CreatedOn { set;get;}
public string Location { set;get;}
}
现在将此视图模型用于您的投影
select new StudentVm { StudentRequestId = sr.StudentRequestId,
ClassName= c.ClassName,
Location = sr.Location,
CreatedOn = sr.CreatedOn }
并确保您的视图不是强类型 PagedList<StudentVm>
@using PagedList;
@model PagedList<YourNamespace.StudentVm>
<table>
@foreach (var item in Model)
{
<tr>
<td>@Html.DisplayFor(a=> item.ClassName)</td>
<td>@Html.DisplayFor(a=> item.Location)</td>
<td>@Html.DisplayFor(a=> item.StudentRequestId)</td>
<td>@Html.DisplayFor(a=> item.CreatedOn)</td>
</tr>
}
</table>
另外,从你的问题来看,你并没有真正使用 PagedList。要仅传递项目列表,您不需要将其转换为 PagedList<T>
。您可以简单地从操作方法发送一个 List<StudentVm>
并将您的视图更改为强类型来执行此操作。如果你真的使用它(用于分页),请使用 PagedList
实际上我想在我的视图页面中集成分页,为此我正在从下面的代码中检索数据,我正在将这段代码从控制器传递给视图但是我遇到的任何问题都是
var model = (from sr in db.StudentRequests
join c in db.Classes
on sr.ClassId equals c.ClassId
select new { sr.StudentRequestId,c.ClassName,sr.CreatedOn,sr.Location,sr.PaymentMethod }).ToList().ToPagedList(page ?? 1, 1);
return View(model);
我遇到了
的问题Type : InvalidOperationException
Message : The model item passed into the dictionary is of type 'PagedList.PagedList`1[<>f__AnonymousType3`5[System.Int32,System.String,System.Nullable`1[System.DateTime],System.String,System.String]]', but this dictionary requires a model item of type 'PagedList.IPagedList`1[Student_Tutor.Models.StudentRequest]'.
Source : System.Web.Mvc
我的视角是
@using PagedList;
@using PagedList.Mvc;
@model IPagedList<Student_Tutor.Models.StudentRequest>
@if (ViewBag.StudentRequest != null)
{
var StudentRequestId = (int)Model.First().StudentRequestId;// Here I am able to get the StudentRequestId
var StudentRequestTimecount = StudentRequestTime.Where(d => d.StudentRequestId == StudentRequestId).ToList();
var TutorStudentRequestcount = TutorStudentRequest.Where(d => d.StudentRequestId == StudentRequestId).ToList();
@Html.Displayfor(model => model.First().StudentRequestId)// here only text is displaying as StudentRequestId
@Html.Displayfor(Model => Model.First().CreatedOn)//here only text is diplaying as created on
}
请解释为什么我会收到此错误?
更新 1
var model = (from sr in db.StudentRequests
join c in db.Classes
on sr.ClassId equals c.ClassId
select new Studentvm{ StudentRequestId = sr.StudentRequestId,ClassName= c.ClassName,
CreatedOn =Convert.ToDateTime(sr.CreatedOn),Location= sr.Location,PaymentMethod= sr.PaymentMethod })
.ToList().ToPagedList(page ?? 1, 1);
return View(model);
但我收到错误
An exception of type 'System.NotSupportedException' occurred in Student_Tutor.dll but was not handled in user code
Additional information: LINQ to Entities does not recognize the method 'System.DateTime ToDateTime(System.Object)' method, and this method cannot be translated into a store expression.
这部分 LINQ 代码
select new { sr.StudentRequestId,c.ClassName,sr.CreatedOn,sr.Location,sr.PaymentMethod }
这是为您从 LINQ 查询获得的结果集合中的每个项目创建一个匿名对象,并且您正在从中创建一个 PagedList
。但是您的视图被强类型化为 PagedList<StudentRequest>
理想的解决方案是创建一个视图模型来表示此视图所需的数据,并将其用于 LINQ 查询的投影部分
public class StudentVm
{
public int StudentRequestId { set;get;}
public string ClassName { set;get;}
public DateTime CreatedOn { set;get;}
public string Location { set;get;}
}
现在将此视图模型用于您的投影
select new StudentVm { StudentRequestId = sr.StudentRequestId,
ClassName= c.ClassName,
Location = sr.Location,
CreatedOn = sr.CreatedOn }
并确保您的视图不是强类型 PagedList<StudentVm>
@using PagedList;
@model PagedList<YourNamespace.StudentVm>
<table>
@foreach (var item in Model)
{
<tr>
<td>@Html.DisplayFor(a=> item.ClassName)</td>
<td>@Html.DisplayFor(a=> item.Location)</td>
<td>@Html.DisplayFor(a=> item.StudentRequestId)</td>
<td>@Html.DisplayFor(a=> item.CreatedOn)</td>
</tr>
}
</table>
另外,从你的问题来看,你并没有真正使用 PagedList。要仅传递项目列表,您不需要将其转换为 PagedList<T>
。您可以简单地从操作方法发送一个 List<StudentVm>
并将您的视图更改为强类型来执行此操作。如果你真的使用它(用于分页),请使用 PagedList