【问题标题】:PagedList<T> does not contain a definition for the properties of my Model <T>PagedList<T> 不包含我的模型 <T> 的属性的定义
【发布时间】:2017-01-13 12:19:39
【问题描述】:

我的部分视图如下:

  @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 不包含 LastName 的定义。

我知道问题是由于学生控制器的 Index 方法引起的,我需要添加 .Select 但我不确定。如果有人可以帮助我,我将不胜感激

【问题讨论】:

    标签: c# asp.net-mvc pagedlist


    【解决方案1】:

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

    将表定义更改为:

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

    一切都会好起来的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-06-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-06
      • 2012-07-04
      • 2021-12-29
      相关资源
      最近更新 更多