我应该如何使用ToPagedList从控制器传递查询 [英] How should I pass my query from controller using ToPagedList

查看:171
本文介绍了我应该如何使用ToPagedList从控制器传递查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际上,我想将分页集成到我的视图页面中,为此,我正在从下面的代码中检索数据,并且正在从控制器传递此代码以进行查看,但是无论我面对什么问题

Actually i want to integrate paging in my view page and for that I am retrieving data from below code and I am passing this code from controller to view but any how I am facing issue as

 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>

That is creating an annonymous object for each item in the result collection you get from your LINQ query and you are creating a PagedList from that. But your view is strongly typed to PagedList<StudentRequest>

理想的解决方案是创建一个视图模型来表示此视图所需的数据,并将其用于LINQ查询的投影部分

The ideal solution is to create a viewmodel to represent the data needed for this view and use that in the projection part of your LINQ query

public class StudentVm
{
  public int StudentRequestId { set;get;}
  public string ClassName { set;get;}
  public DateTime CreatedOn { set;get;}
  public string Location { set;get;}
}

现在使用此视图模型进行投影

Now use this view model for your projection

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

并确保您的视图未严格键入为PagedList<StudentVm>

And make sure your view is not strongly typed to 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>.您可以简单地从action方法发送List<StudentVm>并将您的视图更改为强类型来执行此操作.如果您真的使用PagedList(用于分页)

Also, from your question, you are not really using the PagedList. To pass just a list of items, you do not need to convert that to PagedList<T> . You can simply send a List<StudentVm> from action method and change your view to be strongly typed to do that. Use PagedList if you are really using it (for paging)

这篇关于我应该如何使用ToPagedList从控制器传递查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆