page List:在MVC视图中以不同的方式声明相同的模型

本文关键字:方式 声明 模型 List MVC 视图 page | 更新日期: 2023-09-27 18:14:19

我有以下小的调查视图:

@model IEnumerable<PROJECT.Models.Surveys>
@if (ViewBag.canCreate)
{
    <a href="Surveys/Create" class="btn btn-success btn-sm createButton"><span class="glyphicon glyphicon-pencil"></span> Create a New Survey</a>
}
<table class="table table-striped">
    <tr>
        <th></th>
        <th>
            @Html.DisplayNameFor(model => model.Subject)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.OwnerOrganization.Name)
        </th>
        <th class="hidden-xs hidden-sm">
            @Html.DisplayNameFor(model => model.CreatedDate)
        </th>
        <th class="hidden-xs hidden-sm"></th>
    </tr>
    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.ActionLink("Details", "Details", new { id = item.Id }, htmlAttributes: new { @class = "btn-default btn-sm noDecoration" })
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Subject)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.OwnerOrganization.Name)
            </td>
            <td class="hidden-xs hidden-sm">
                @{
                    string createdDate = item.CreatedDate.Date.ToString("d");
                }
                @createdDate
            </td>
            @if ((User.Identity.IsAuthenticated) && (item.OwnerOrganizationId == ViewBag.currentUserGroupId))
            {
                <th class="hidden-xs hidden-sm">
                    @Html.ActionLink("Edit", "Edit", new { id = item.Id }, htmlAttributes: new { @class = "btn-success btn-sm noDecoration" })
                </th>
            }
        </tr>
    }
</table>

我正在尝试添加相同的页面列表功能,我对成员项目的视图调查视图:

@model PagedList.IPagedList<PROJECT.Models.MemberProjects>
@using PagedList.Mvc
....
<div class="paginationWrapper">
    <div class="leftDiv">
        @Html.PagedListPager(Model, page => Url.Action("Index", new { page = page, pageSize = ViewBag.pageSize, noSearch = true }))
    </div>
    <div class="rightDiv">
        @Html.Partial("_Pagination")
    </div>
</div>

当我尝试实现相同的时候,我的属性被标记说它们不包含属性的定义,也没有属性的扩展方法接受类型为PagedList.IPagedList<Project.Models.Surveys>的第一个参数。

@model IEnumerable<PROJECT.Models.Surveys>
@model PagedList.IPagedList<PROJECT.Models.Surveys>
@using PagedList.Mvc
@if (ViewBag.canCreate)
{
    <a href="Surveys/Create" class="btn btn-success btn-sm createButton"><span class="glyphicon glyphicon-pencil"></span> Create a New Survey</a>
}
<table class="table table-striped">
    <tr>
        <th></th>
        <th>
            /* Error */
            @Html.DisplayNameFor(model => model.Subject)
        </th>
        <th>
            /* Error */
            @Html.DisplayNameFor(model => model.OwnerOrganization.Name)
        </th>
        <th class="hidden-xs hidden-sm">
            /* Error */
            @Html.DisplayNameFor(model => model.CreatedDate)
        </th>
        <th class="hidden-xs hidden-sm"></th>
    </tr>
    ....
</table>
<div class="paginationWrapper">
    <div class="leftDiv">
        @Html.PagedListPager(Model, page => Url.Action("Index", new { page = page, pageSize = ViewBag.pageSize, noSearch = true }))
    </div>
    <div class="rightDiv">
        @Html.Partial("_Pagination")
    </div>
</div>

显然,这是因为我试图以两种不同的方式指定模型,但我不太确定如何最好地绕过它。谁能给点建议?我对MVC开发还是相当陌生的

page List:在MVC视图中以不同的方式声明相同的模型

我也有类似的问题。为此,我建议您仅在控制器中创建PagedList。您将在视图上显示调查列表。因此,而不是IEnumerable<PROJECT.Models.Surveys>创建您的模型视图在控制器这样一种方式,它支持PagedList。因此,我将向您展示如何在控制器中为PagedList制作模型。现在假设你有这样的模型:

public class Survey()
{
  public string Subject{get;set;}
  public Organization org{get;set;}
  public DateTime CreatedDate{get;set;}
}

现在您正在制作该型号的IEnumerable列表。因此,在控制器中执行以下操作而不是IEnumerable列表。创建另一个模型,假设SurveyPagedListModel如下:

public class SurveyPagedListModel()
{
   public IPagedList<Survey> pagedListSurvey{get;set;}  
} 

现在在你的视图中这样做:

 @model SurveyPagedListModel
// Your view code goes here 
 ..................
// Place this code as per your settings where you want to set pagination
 <div class="page_numbers page_numbers-sec">
      <div class="pagination">
      @(Model.pagedListSurvey.PageCount < Model.pagedListSurvey.PageNumber ? 0 :         Model.pagedListSurvey.PageNumber)
      @(Model.pagedListSurvey.PageCount)
      @Html.PagedListPager(Model.pagedListSurvey, page => Url.Action("Index")
      </div>
 </div> 
css

.page_numbers 
{
    color: #333333;
    display: block;
    float: left;
    font-size: 12px;
    font-weight: bold;
    padding: 10px 0 0 15px;
}
.page_numbers-sec{
    color: #333333; 
    font-weight: normal; 
    font-size: 13px;
}

分页是Bootstrap css。