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

时间:2016-08-29 11:04:05

标签: c# asp.net-mvc asp.net-mvc-4 pagedlist

嗨,我有一个链接按钮。当我点击链接按钮时,将显示记录数。我想为此应用分页。我试过如下。

Index.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);
}

这是我的观看代码

@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 to detailsbyclientid action方法。如何从actionlink发送clientid currentfilter?我目前正在获得可以为空的错误,因为我没有发送当前的过滤器。如果我在任何地方都错了,请告诉我。非常感谢

1 个答案:

答案 0 :(得分:0)

currentFilter的参数更改为int?(可为空),然后您可以测试其null是否为<td>@Html.ActionLink(group.clientId.ToString(), "detailsbyClientId", "DocumentVerification", new { clientId = group.clientId },null)</td> 。也不是你链接可以只是

detailsbyclientIdviewModel

但是,代码中还有多个其他错误会引发异常。

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

detailsbyclientId

接下来,您将PagedList分配给属性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 方法可以使用它。

最后,您需要在寻呼机方法中引用该属性

detailsbyclientIdviewModel

话虽如此,当您从未使用该模型的int upldIdIEnumerable<Metadata> metadata时,不清楚为什么将IPagedList<detailsbyClientId> pagedLog = detailsbyclient.ToPagedList(pageNumber, pageSize); return View("detailsbyClientId", pagedLog ); 模型传递给视图。您可以将原始代码保存在视图和控制器中

URL