我应该如何使用ToPagedList从控制器传递我的查询

时间:2017-11-06 17:05:39

标签: asp.net-mvc asp.net-mvc-4 asp.net-mvc-3

实际上我想在我的视图页面中集成分页,为此我从下面的代码中检索数据,我将这个代码从控制器传递到视图但是我面临的问题是

 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.

1 个答案:

答案 0 :(得分:1)

这部分LINQ代码

select new { sr.StudentRequestId,c.ClassName,sr.CreatedOn,sr.Location,sr.PaymentMethod }

这是为您从LINQ查询获得的结果集合中的每个项目创建一个匿名对象,并且您正在从中创建PagedList。但您的观点强烈输入PagedList<StudentRequest>

理想的解决方案是创建一个viewmodel来表示此视图所需的数据,并在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