简体   繁体   English

ASP.NET MVC - Model 传递的项目是 Generic.List 但需要类型 PagedList

[英]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.我对这一切都很陌生,我在 App.net 4.7.2 MVC 应用程序中遇到了分页问题。 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]'." "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.我想我理解这个问题,因为我在 controller 中使用了一个通用列表,但我只是不确定如何解决它。 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: Controller部分:

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: 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解决方案是您在 controller 和视图中都使用IList接口,问候

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!感谢所有的回复!

暂无
暂无

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

相关问题 ASP.NET MVC:传递到词典中的模型项的类型为“ ClassA”,但是此词典需要类型为“ ClassA”的模型项 - ASP.NET MVC: The model item passed into the dictionary is of type 'ClassA', but this dictionary requires a model item of type 'ClassA' 传递到字典中的模型项在ASP.net MVC中的类型为“ System.Collections.Generic.List…” - The model item passed into the dictionary is of type 'System.Collections.Generic.List… in ASP.net MVC ASP.Net MVC“传入字典的模型项是&#39;System.Collections.Generic.List&#39;类型” - ASP.Net MVC “The model item passed into the dictionary is of type 'System.Collections.Generic.List” 在我面前回答吗? (Generic.List需要类型为Generic.IEnumerable Error的模型项) - Answer Right in Front of Me? (Generic.List requires a model item of type Generic.IEnumerable Error) 在构建 ASP.NET Core Web 应用程序时,我得到“传递到字典中的模型项的类型为‘System.Collections.Generic.List`1” - While building ASP.NET Core web app I get "The model item passed into the dictionary is of type 'System.Collections.Generic.List`1" 传递到字典中的模型项的类型为&#39;System.Collections.Generic.List,但是此字典需要类型为的模型项 - model item passed into the dictionary is of type 'System.Collections.Generic.List, but this dictionary requires a model item of type 传递到字典中的模型项需要类型为&#39;PagedList.IPagedList的模型项 - The model item passed into the dictionary requires a model item of type 'PagedList.IPagedList ASP.NET MVC - 获取异常:InvalidOperationException:传递到 ViewDataDictionary 的 model 项目是类型 - ASP.NET MVC - Get exception: InvalidOperationException: The model item passed into the ViewDataDictionary is of type 在ASP.NET MVC中使用带有连接两个模型的PagedList - Using a PagedList with Join two model in asp.net mvc DropDownList Model 绑定 ASP.NET PagedList 视图中的 MVC - DropDownList Model Binding ASP.NET MVC in a PagedList View
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM