Search code examples
c#asp.net-mvcpagedlist

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


I have this partial View as below:

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

My Index.cshmtl is :

  @model IPagedList<Student>

 @{
   ViewBag.Title = "Index";
 }

@Html.Partial("_Student", Model)

My StudentController is

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

The problem happens to "@Html.DisplayNameFor(model => model.LastName)". It is saying that " IPaged does not contatin a definition for LastName.

I have an idea that the problem is caused because to the Index method of Student Controller I need to add .Select but I am not sure for that. I would really appreciate if someone can help me


Solution

  • It's not working, because overloaded method which you want to use doesn't fit with your interface. It's for IEnumerable<> types. You can access to the first element in the list and it should returns you correct value even if list is empty.

    Change your table definition to:

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

    and everything will be ok.