Asp.net mvc MVC4分页错误

Asp.net mvc MVC4分页错误,asp.net-mvc,asp.net-mvc-4,Asp.net Mvc,Asp.net Mvc 4,使用这个控制器,我得到了错误 传入字典的模型项的类型为“System.Collections.Generic”,但此字典需要“PagedList.IPagedList”类型的模型项 my view index.cshtml是: @using PagedList; @using PagedList.Mvc; @model IPagedList<LiteracyPayroll.Models.AdulLiteracyTeachers> @{ ViewBag.Title = "I

使用这个控制器,我得到了错误

传入字典的模型项的类型为“System.Collections.Generic”,但此字典需要“PagedList.IPagedList”类型的模型项

my view index.cshtml是:

@using PagedList;
@using PagedList.Mvc;

@model IPagedList<LiteracyPayroll.Models.AdulLiteracyTeachers>

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>


 @using (Html.BeginForm("Index","AdultLiteracyTeachers",FormMethod.Get))
{ 
<p>
Find By Teacher Name: @Html.TextBox("searchstring",ViewBag.CurrentFilter as string)
<input type="submit" value="Search" />
 </p>



} 




<table>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.First().District.DistName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.First().ALTCLC)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.First().ALTGEN)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.First().ALTCNIC)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.First().ALTDOB)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.First().ALTName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.First().ALTFName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.First().ALTAppNo)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.First().ALTDes)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.First().ALTQuali)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.First().ALTexp)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.First().ALTDom)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.First().ALTdoc)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.First().ALTBG)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.First().ALTAddress)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.First().ALTcontact)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.First().ALTBankno)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.First().ALTBankname)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.First().ALTBcode)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.First().ALTNTN)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.First().ALTPay)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.First().Image)
        </th>
        <th></th>
    </tr>

@foreach (var item in Model) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.District.DistName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.ALTCLC)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.ALTGEN)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.ALTCNIC)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.ALTDOB)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.ALTName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.ALTFName)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.ALTAppNo)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.ALTDes)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.ALTQuali)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.ALTexp)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.ALTDom)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.ALTdoc)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.ALTBG)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.ALTAddress)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.ALTcontact)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.ALTBankno)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.ALTBankname)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.ALTBcode)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.ALTNTN)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.ALTPay)
        </td>


            <td>
               <img src="@Url.Content("~/Content/Uploads/" +  "/" + item.Image + "")" alt="@item.Image" width="100px" height="100px" />
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.ALTid }) |
            @Html.ActionLink("Details", "Details", new { id=item.ALTid }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.ALTid })
        </td>
    </tr>
}

</table>
<div>
Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) Of @Model.PageCount  
@Html.PagedListPager(Model, page => Url.Action("Index", new { page })) 
</div>

当您检查searchstring和currentFilter是否为空时,问题就在下面一行。现在看起来是这样的-

public ViewResult Index(string searchstring, string currentFilter, int? page)
    {
        if (searchstring == null && currentFilter == null && !page.HasValue)
        {
            // No parameters
            var adulliteracyteachers = db.AdulLiteracyTeachers.Include(a => a.District);
            return View(adulliteracyteachers.ToList());
        }

        // Regular code here down
        blah... blah..
     }
更改您的代码,改为说应该修复它:

// Initialize the pagesize and pageNo at the starting of the function
    int pagesize = 10;
    int pageNo = (page ?? 1);

    if (searchstring == null && currentFilter == null && !page.HasValue)
    {
        // No parameters
        var adulliteracyteachers = db.AdulLiteracyTeachers.Include(a => a.District);
        return View(adulliteracyteachers.ToPagedList(pageNo, pagesize));
    }

    // Regular code here down
    .......

能否将属性HttpGet和/或HttpPost放入索引操作中。如果您有2个,我认为其中一个没有通过视图中的正确模型显示Return ViewAdAlliteracyTeachers.ToList;->该视图需要IPagedList类型的模型
// Initialize the pagesize and pageNo at the starting of the function
    int pagesize = 10;
    int pageNo = (page ?? 1);

    if (searchstring == null && currentFilter == null && !page.HasValue)
    {
        // No parameters
        var adulliteracyteachers = db.AdulLiteracyTeachers.Include(a => a.District);
        return View(adulliteracyteachers.ToPagedList(pageNo, pagesize));
    }

    // Regular code here down
    .......