这是错误:传入字典的模型项的类型为'PagedList.PagedList ' 1[abcd.Models.xy

本文关键字:PagedList xy Models abcd 类型 错误 模型 字典 | 更新日期: 2023-09-27 18:08:29

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

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


下面是完整的错误信息:

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

这是错误:传入字典的模型项的类型为'PagedList.PagedList ' 1[abcd.Models.xy

您的模型例外

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

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

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