简体   繁体   English

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

[英]Unable to apply paging in mvc4 when using viewmodel and actionlink

Hi I have one link button. 嗨,我有一个链接按钮。 When i clicked on link button number of records will be displayed. 当我点击链接按钮时,将显示记录数。 I want to apply paging for that. 我想为此应用分页。 I have tried as below. 我试过如下。

Index.cshtml 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>
}

This is my controller code. 这是我的控制器代码。

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

This is my 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>
}

my viewmodel 我的viewmodel

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

I am sending clientid to detailsbyclientid action method. 我发送clientid to detailsbyclientid action方法。 How can I send clientid as currentfilter from actionlink? 如何从actionlink发送clientid作为currentfilter I am getting nullable error currently because I am not sending current filter. 我目前正在获得可以为空的错误,因为我没有发送当前的过滤器。 Let me know if I am wrong in any place. 如果我在任何地方都错了,请告诉我。 Thank you lot 谢谢你

Change your parameter for currentFilter to int? currentFilter的参数更改为int? (nullable), and then you can just test if its null or not. (可以为空),然后你可以测试它是否为null Not also that you link can be just 也不是你链接可以只是

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

However there are multiple other errors in your code that will throw exceptions. 但是,代码中还有多个其他错误会引发异常。

First, the model your returning to the view is typeof detailsbyclientIdviewModel , therefore the model in the view must match 首先,返回视图的模型是detailsbyclientIdviewModel ,因此视图中的模型必须匹配

@model yourAssembly.detailsbyclientIdviewModel

Next, you are assigning the PagedList to property detailsbyclientId so you need to change the model to 接下来,您将PagedList分配给属性detailsbyclientId因此您需要将模型更改为

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

so that it can be used by the @Html.PagedListPager() method. 这样它就可以被@Html.PagedListPager()方法使用。

Finally, you need to refer to that property in the pager methods 最后,您需要在寻呼机方法中引用该属性

@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

Having said that, its unclear why your passing a detailsbyclientIdviewModel model to the view when you never use the int upldId or IEnumerable<Metadata> metadata of that model. 话虽如此,当你从不使用该模型的int upldIdIEnumerable<Metadata> metadata时,还不清楚为什么你将detailsbyclientIdviewModel模型传递给视图。 You could just keep the original code in your view and in the controller 您可以将原始代码保留在视图和控制器中

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM