当使用viewmodel和actionlink时,无法在mvc4中应用分页

本文关键字:mvc4 分页 应用 viewmodel actionlink | 更新日期: 2023-09-27 18:04:26

你好,我有一个链接按钮。当我点击链接按钮,记录的数量将显示。我想申请分页。我试过如下方法。

索引。cshtml

@foreach (var group in Model.records)
{
    <tr>                    
        <td>@Html.ActionLink(@group.clientId.ToString(), "detailsbyClientId", "DocumentVerification", new { clientId = @group.clientId.ToString()},null)</td>
        <td>@group.clientName</td>
        <td>@group.Count</td>
    </tr>
}

这是我的控制器代码。

public ActionResult detailsbyClientId(int? clientId, int currentFilter, int? page)
{
    if (clientId != null)
    {
        page = 1;
    }
    else
    {
        clientId = currentFilter;
    }
    ViewBag.CurrentFilter = clientId;
    int pageSize = 8;
    int pageNumber = (page ?? 1);
    documentVerificationBAL objBAL = new documentVerificationBAL();
    int cId = Convert.ToInt32(clientId);
    List<detailsbyClientId> detailsbyclient = objBAL.detailsbyclient(cId);
    IPagedList<detailsbyClientId> pagedLog = detailsbyclient.ToPagedList(pageNumber, pageSize);
    detailsbyclientIdviewModel model;
    model = new detailsbyclientIdviewModel()
    {
        detailsbyclientId = pagedLog
    };
    return View("detailsbyClientId", model);
}

这是我的viewcode

@model  PagedList.IPagedList<c3card.DAL.detailsbyclientIdviewModel>
@using PagedList.Mvc;
@if(!Model.detailsbyclientId.Any())
{
<div>
    <table width="100%" border="0" cellspacing="0" cellpadding="0" class="dataTable tableHover">
        <tr>
            <th>Corporate Name</th>
            <th>employee ID</th>
            <th>employee Name</th>
            <th>Nationality</th>
            <th>Document Type</th>
            <th>Actions</th>
        </tr>
        @foreach (var group in Model.detailsbyclientId)
        {
            <tr>
                <td> @group.clientName </td>
                <td> @group.employeeId </td>
                <td> @group.employeeName </td>
                <td> @group.documentType </td>
                <td scope="col">
                    <input type="button" class="btn btn-primary btn-cons" value="View Document" onclick="showDocumentData('@group.upld_Id');" />
                </td>
                <td scope="col">
                    < input type="button" class="btn btn-primary btn-cons" value="Approve" onclick="showDocumentData('@group.upld_Id');" />
                </td>
                <td scope="col">
                    < input type="button" class="btn btn-primary btn-cons" value="Reject" onclick="showDocumentData('@group.upld_Id');" />
                </td>
            </tr>
        }
    </table>
    @Html.PagedListPager(Model, page => Url.Action("detailsbyClientId",
    new { page, currentFilter = ViewBag.CurrentFilter, pageSize = 5 }))
    Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount
</div>
}
我viewmodel

public class detailsbyclientIdviewModel
    {
        public int upldId { get; set; }
        public IEnumerable<detailsbyClientId> detailsbyclientId { get; set; }
        public IEnumerable<Metadata> metadata { get; set; }
    }

我正在发送clientid到detailsbyclientid动作方法。如何从actionlink发送clientid作为currentfilter ?我得到了可空错误,因为我没有发送当前过滤器。如果我错了,请告诉我。谢谢大家

当使用viewmodel和actionlink时,无法在mvc4中应用分页

currentFilter的参数更改为int?(可空),然后您可以测试它是否为null。并不是说你的链接只能是

<td>@Html.ActionLink(group.clientId.ToString(), "detailsbyClientId", "DocumentVerification", new { clientId = group.clientId },null)</td>

然而,在你的代码中有许多其他的错误会抛出异常。

首先,返回给视图的模型的类型是detailsbyclientIdviewModel,因此视图中的模型必须匹配

@model yourAssembly.detailsbyclientIdviewModel

接下来,将PagedList分配给属性detailsbyclientId,因此需要将模型更改为

public class detailsbyclientIdviewModel
{
    ....
    public IPagedList<detailsbyClientId> detailsbyclientId { get; set; }
}

,以便@Html.PagedListPager()方法可以使用它。

最后,您需要在分页方法

中引用该属性。
@Html.PagedListPager(Model.detailsbyclientId, page => Url.Action("detailsbyClientId",
    new { page, currentFilter = ViewBag.CurrentFilter, pageSize = 5 }))
Page @(Model.detailsbyclientId.PageCount < Model.detailsbyclientId.PageNumber ? 0 : Model.PageNumber) of @Model.detailsbyclientId.PageCount

话虽如此,当你从不使用该模型的int upldIdIEnumerable<Metadata> metadata时,不清楚为什么你将detailsbyclientIdviewModel模型传递给视图。你可以将原始代码保留在视图和控制器

IPagedList<detailsbyClientId> pagedLog = detailsbyclient.ToPagedList(pageNumber, pageSize);
return View("detailsbyClientId", pagedLog );