PagedList<T> 不包含我的模型 <T> 属性的定义

PagedList<T> does not contain a definition for the properties of my Model <T>

我有如下部分视图:

  @model IPagedList<Student>

 <h2>List of Students</h2>


<div id="studentList">

    <div class="pagedList" data-uwa-target="#studentList">

          @Html.PagedListPager(Model,page=>Url.Action("Index",new  { page }),
         PagedListRenderOptions.MinimalWithItemCountText)


    </div>

    <table class="table">

        <tr>
        <th>
            @Html.DisplayNameFor(model => model.LastName) //here error
        </th>
        <th>
            @Html.DisplayNameFor(model => model.FirstName) //here error
        </th>
        <th>
            @Html.DisplayNameFor(model => model.City) //here error
        </th>
        <th></th>
    </tr>


        @foreach (var item in Model)
        {

            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.LastName)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.FirstName)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.City)
                </td>
            </tr>

        }

    </table>



        <div class="row">

            <div class="col-sm-6" id="slid">
                <button id="export" class="btn btn-default btn-primary" onclick="location.href='@Url.Action("ExportInCSVFormat")';return false;">Export Student List in CSV Format</button>
            </div>

            <div class="col-sm-4" id="excelSlide">
                <button id="excelExport" class="btn btn-default btn-success" onclick="location.href='@Url.Action("ExportStudentsInExel")';return false;">Export Student List in Excel Format</button>
            </div>

        </div>

我的 Index.cshmtl 是:

  @model IPagedList<Student>

 @{
   ViewBag.Title = "Index";
 }

@Html.Partial("_Student", Model)

我的 StudentController 是

   public ActionResult Index(int page=1)
      {
          var model = dbAllStudents.Students
                                .OrderByDescending(p => p.LastName)
                                .ToPagedList(page, 10);
           return View(model);
       }

问题发生在“@Html.DisplayNameFor(model => model.LastName)”。它是说“IPaged 不包含姓氏的定义。

我知道问题是因为我需要在 Student Controller 的 Index 方法中添加 .Select 但我不确定。如果有人能帮助我,我将不胜感激

它不起作用,因为您要使用的重载方法不适合您的界面。它适用于 IEnumerable<> 类型。您可以访问列表中的第一个元素,即使列表为空,它也应该 returns 您更正值。

将您的 table 定义更改为:

<table class="table">

      <tr>
      <th>
          @Html.DisplayNameFor( => Model[0].LastName)
      </th>
      <th>
          @Html.DisplayNameFor(_ => Model[0].FirstName)
      </th>
      <th>
          @Html.DisplayNameFor(_ => Model[0].City)
      </th>
      <th></th>
      </tr>

    @foreach (var item in Model)
    {
      <tr>
          <td>
              @Html.DisplayFor(_ => item.LastName)
          </td>
          <td>
              @Html.DisplayFor(_ => item.FirstName)
          </td>
          <td>
              @Html.DisplayFor(_ => item.City)
          </td>
      </tr>
    }
</table>

一切都会好起来的。