如何在asp.net mvc 5中解决这个分页错误?

时间:2014-10-29 13:10:21

标签: c# asp.net-mvc asp.net-mvc-5

我有一个asp.net MVC 5视图。我有以下代码:

@model PagedList.IPagedList<parishV3.Models.HeadsViewModel>
@using PagedList;
@using PagedList.Mvc;
<link href="~/Content/css/PagedList.css" rel="stylesheet" />

@using (Html.BeginForm("Index", "Heads", FormMethod.Get))
{
  <p>
    <b> Find by name:</b>@Html.RadioButton("findBy", "fname") <text>First Name</text>
    @Html.RadioButton("findBy", "lname")<text>Last Name</text><br />
    @Html.RadioButton("findBy", "h_no")<text>Header Number</text><br />
    @Html.TextBox("Seach_Data", ViewBag.FilterValue as string, ViewBag.CurrentFilter as string)
    <input type="submit" value="Find" />
</p>
}

<br />
Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount
@Html.PagedListPager(Model, page => Url.Action("Index", new { page, findby =  Request.QueryString["findby"], Seach_Data = Request.QueryString["Seach_Data"] }))

控制器

public ActionResult Index(string Seach_Data, string findby, int? page, string currentFilter )
{
    if (findby == "fname")
    {
       string query = "SELECT v.h_no as h_no, v.h_initials , v.h_id as h_id , t.titles as titles,     p.Suburb as Suburb, p.flatNo as flatNo, p.flatName as flatName,v.Email as Email, v.cell as cell,    v.tel_h as tel_h, v.fname as fname, p.strname as strname, p.strNo as strNo, p.City as City, LEFT(v.lname, 255) as lname from [dbo].[Heads] v, [dbo].[Addresses1] p, [dbo].[Titles] t WHERE v.[h_ID] = p.[h_id] and v.[title_id]= t.[title_id] ";

       var ViewModel = db.Database.SqlQuery<HeadsViewModel>(query);
       return View(ViewModel.Where(x => x.fname == Seach_Data).ToList().ToPagedList(page ?? 1, 4));
    }
    else if (findby == "lname")
    {
        string query = "SELECT v.h_no as h_no, v.h_initials , v.h_id as h_id , t.titles as titles, p.Suburb as Suburb, p.flatNo as flatNo, p.flatName as flatName,v.Email as Email, v.cell as cell,  v.tel_h as tel_h,  v.fname as fname, p.strname as strname, p.strNo as strNo, p.City as City, LEFT(v.lname, 255) as lname from [dbo].[Heads] v, [dbo].[Addresses1] p, [dbo].[Titles] t WHERE v.[h_ID] = p.[h_id] and v.[title_id]= t.[title_id] "; 
        var ViewModel = db.Database.SqlQuery<HeadsViewModel>(query);

        return View(ViewModel.Where(x => x.lname == Seach_Data).ToList().ToPagedList(page ?? 1, 6));  
    }
    else
    {
      string query = "SELECT v.h_no as h_no, v.h_initials , v.h_id as h_id , t.titles as titles, p.Suburb as Suburb, p.flatNo as flatNo, p.flatName as flatName,v.Email as Email, v.cell as cell,  v.tel_h as tel_h,  v.fname as fname, p.strname as strname, p.strNo as strNo, p.City as City, LEFT(v.lname, 255) as lname from [dbo].[Heads] v, [dbo].[Addresses1] p, [dbo].[Titles] t WHERE v.[h_ID] = p.[h_id] and v.[title_id]= t.[title_id] ";
      var ViewModel = db.Database.SqlQuery<HeadsViewModel>(query);
      return View(ViewModel);
    }
}

我的问题是,当我运行项目时,它会给我一个错误,上面写着:

  

System.InvalidOperationException:传递到字典中的模型项是类型   System.Data.Entity.Infrastructure.DbRawSqlQuery 1[parishV3.Models.HeadsViewModel], but this dictionary requires a model item of type 'PagedList.IPagedList 1 [parishV3.Models.HeadsViewModel]&#39;&#34;

1 个答案:

答案 0 :(得分:1)

在初始页面加载中,findby的值为null,因此执行else块并返回DbRawSqlQuery1<parishV3.Models.HeadsViewModel>,但您的视图要求模型为IPagedList1<parishV3.Models.HeadsViewModel>。将else阻止更改为

else
{
  string query = ".....";
  var ViewModel = db.Database.SqlQuery<HeadsViewModel>(query);
  return View(ViewModel.ToList().ToPagedList(1, 6));
}

注意使用分页列表时,查询应包含OrderBy子句,以确保始终以相同的顺序返回集合,否则可能会得到不可预测的结果。