简体   繁体   中英

ASP.NET MVC - Model Item passed is Generic.List but requires type PagedList

I'm new to all this and I'm having trouble with Paging in an App.net 4.7.2 MVC application. I get the following error:

"The model item passed into the dictionary is of type 'System.Collections.Generic.List 1[AIAR.Models.PIAModel]', but this dictionary requires a model item of type 'PagedList.IPagedList 1[AIAR.Models.PIAModel]'."

I think I understand the problem, as I'm using a generic list within my controller, but I'm just not sure how to resolve it. I've been looking through all the Googles for some time now and just can't figure it out. Any help would be much appreciated. Please let me know if I need to provide anything else.

Controller section:

public ActionResult ViewPIAS(string searchBy, string search,int? page)
        {
            ViewBag.Message = "PIA List";

            var data = LoadPIAS();
            List<PIAModel> PIAS = new List<PIAModel>();

            foreach (var row in data)
            {
                PIAS.Add(new PIAModel
                {
                    Id = row.Id,
                    AssetName = row.AssetName,
                    AssetDescription = row.AssetDescription,
                    Unit = row.Unit,
                    InformationAssetCustodian = row.InformationAssetCustodian

                });

            }
            if (searchBy == "AssetName")
            {
                return View(PIAS.Where(x => x.AssetName.StartsWith(search)).ToPagedList(page ?? 1, 3).ToList());
            }
            else if(searchBy == "AssetDescription")
            {
                return View(PIAS.Where(x => x.AssetDescription.StartsWith(search)).ToPagedList(page ?? 1, 3).ToList());
            }
            else
                return View(PIAS);
        }

View:

@model IPagedList<AIAR.Models.PIAModel>
@using PagedList;
@using PagedList.Mvc;

@{
    ViewBag.Title = "ViewPIAS";
}

<h2>ViewPIAS</h2>

<p>
    @Html.ActionLink("Create New", "NewPIA")
</p>
<p>
    @using (Html.BeginForm("ViewPIAS", "PIA", FormMethod.Get))
    {
        <div>@Html.ActionLink("Return Search Defaults", "ViewPIAS")</div>
        <b>Search By:</b>
        @Html.RadioButton("searchBy", "AssetName") <text>AssetName</text> @Html.RadioButton("searchBy", "AssetDescription") <text>Asset Description</text><br />
        @Html.TextBox("search")<input type="submit" value="Search" />
    }
</p>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.First().Id)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.First().AssetName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.First().AssetDescription)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.First().Unit)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.First().InformationAssetCustodian)
        </th>
    </tr>

    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Id)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.AssetName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.AssetDescription)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Unit)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.InformationAssetCustodian)
            </td>
            <td>
                @Html.ActionLink("Edit", "EditPIA", new { id = item.Id }) |
                @Html.ActionLink("Details", "ViewPIA", new { id = item.Id }) |
                @Html.ActionLink("Delete", "DeletePIA", new { id = item.Id })
            </td>
        </tr>
    }

</table>
@Html.PagedListPager(Model, page => Url.Action("ViewPIAS", new { page }))

Model:

namespace AIAR.Models
{
    public class PIAModel
    {

        [Display(Name = "Id")]
        public int Id { get; set; }

        [Display(Name = "Asset Name")]
        public string AssetName { get; set; }

        [Display(Name = "Asset Description")]
        public string AssetDescription { get; set; }

        [Display(Name = "Unit")]
        public string Unit { get; set; }

        [Display(Name = "Information Asset Custodian")]
        public string InformationAssetCustodian { get; set; }
    }
}

Thanking you!

the solution is that you use the IList interface both in controller and in the view, greetings

Managed to get this working the other day guys.

 int recordsPerPage = 10;
            var list = PIAS.ToList().ToPagedList(page, recordsPerPage);

            if (searchBy == "AssetName")
            {
                var assetnamesearch = list.Where(x => x.AssetName.Contains(search));
                return View(assetnamesearch.ToList().ToPagedList(page, recordsPerPage));
            }
            else
                return View(list);
            }

Thanks for all the responses!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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