MVC 4分页错误

时间:2014-04-22 08:08:42

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

使用此控制器我收到错误

  

传入字典的模型项的类型是System.Collections.Generic,但是这个字典需要一个类型为' PagedList.IPagedList`的模型项

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
    if (searchstring != null)
    {
        page = 1;
    }
    else
    {
        searchstring = currentFilter;
    }
    ViewBag.CurrentFilter = searchstring;
    var teachers = from r in db.AdulLiteracyTeachers select r;
    if (!string.IsNullOrEmpty(searchstring))
    {

        teachers = teachers.Where(r => r.ALTName.ToUpper().Contains(searchstring.ToUpper()));

    }

    teachers = teachers.OrderBy(r => r.ALTName);
    int pagesize = 10;
    int pageNo = (page ?? 1);

    return View(teachers.ToPagedList(pageNo, pagesize));
}

我的观点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>

1 个答案:

答案 0 :(得分:0)

当你检查&#34; searchstring &#34;&#34; searchstring &#34;和&#34; currentFilter &#34;一片空白。目前它看起来像这样 -

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
    .......