【问题标题】:This is the Error : The model item passed into the dictionary is of type 'PagedList.PagedList`1[abcd.Models.xyz]'这是错误:传递到字典中的模型项的类型为“PagedList.PagedList`1[abcd.Models.xyz]”
【发布时间】:2016-09-23 05:45:58
【问题描述】:

//控制器看起来像这样:-

namespace BusRentalSystem.Controllers
{
    public class LocationsController : Controller
    {
        private RentalData db = new RentalData();
        // GET: Locations

        public ActionResult Display(string SearchBy, string search, int? page, string sortBy)
        {
            ViewBag.ArDescription = string.IsNullOrEmpty(sortBy) ? "Ar Desc" : "";
            ViewBag.FrDescription = sortBy == "FrDescription" ? "Fr Desc" : "FrDescription";
            var locations = db.Locations.AsQueryable();  /// to be able to query from this variable
            if (SearchBy == "FrDescription")
            {

                locations = locations.Where(x => x.FrDescription.Contains(search) || search == null);

            }
            else
            {
                locations = locations.Where(x => x.ArDescription.Contains(search) || search == null);
            }

            switch (sortBy)
            {
                case "Ar Desc":
                    locations = locations.OrderByDescending(s => s.ArDescription);
                    break;
                case "Fr Desc":
                    locations = locations.OrderByDescending(s => s.FrDescription);
                    break;
                case "FrDescription":
                    locations = locations.OrderBy(s => s.FrDescription);
                    break;
                default:
                    locations = locations.OrderBy(s => s.ArDescription);
                    break;
            }

            return View("Display", locations.ToPagedList(page ?? 1, 1));
        }
}

// 视图如下所示:-

@foreach (var Locations in Model)
{

    <td>
        @Html.DisplayFor(ModelItem => Locations.ArDescription)
    </td>

    <td>
        @Html.DisplayFor(ModelItem => Locations.FrDescription)

    </td>


        <td>
            @Html.ActionLink("Edit", "Edit", new { id = Locations.Id }) |
            @Html.ActionLink("Details", "Details", new { id = Locations.Id }) |
            @{
                <span onclick="return confirm('Are you sure to delete?')">
                    <a href="/Locations?Delete=@Locations.Id" class="delLink" style="color:red;">
                        @Html.ActionLink("Delete", "Delete", new { id = Locations.Id })
                    </a>
                </span>

            }
        </td>
                }


这是完整的错误信息:

传入字典的模型项是类型 'PagedList.PagedList'1[BusRentalSystem.Models.Location]',但这 字典需要类型的模型项 'PagedList.IPagedList'1[BusRentalSystem.Models.Locations]'。

【问题讨论】:

  • 为什么要使用随机语言标记?
  • 我对 ASP 了解不多,但在this question 他们也使用分页列表。也许您可以发现您的代码与他们的代码之间存在明显差异,例如视图中缺少 @model 标记...
  • 该消息是不言自明的。您将Location 的集合传递给需要Locations 集合的视图(它们不是同一类型)。显示相关代码。
  • 这是 Location 的定义方式: namespace BusRentalSystem.Models { using System;使用 System.Collections.Generic;公共部分类位置 { 公共 int Id { 获取;放; } 公共字符串 ArDescription { 获取;放; } 公共字符串 FrDescription { 获取;放; } } }
  • 编辑您的问题!并且您需要显示您在视图中使用的@model ??? 的声明

标签: c# asp.net-mvc


【解决方案1】:

您的模型除外

@model PagedList.IPagedList<BusRentalSystem.Models.Location>

将您的视图代码改为这样应该可以修复它:

@model PagedList.IPagedList<BusRentalSystem.Models.Location>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-19
    • 2018-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多