这本字典需要类型为的模型项

本文关键字:模型 类型 字典 | 更新日期: 2023-09-27 18:00:13

学习如何围绕c#和剃刀工作。我有这个问题,我正在努力解决。我研究了这个论坛来理解,但没有用。模型项的类型为CookMeIndexViewModel,但需要IEnumerable<CookMeIndexViewModel>

无论如何,这是我一直在使用的代码:然而,我的数据来自我的VehicleClass。我的车辆类别

using LogViewer.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace LogViewer.Classes
{
    public class VehicleClass
    {
        public int id { get; set; }
        public String make { get; set; }
        public String type { get; set; }
        public byte taxBand { get; set; }
        public DateTime created { get; set; }
        public DateTime updated { get; set; }
        public DateTime deleted { get; set; }
        public bool isDeleted { get; set; }
        public decimal price { get; set; }
        public String description { get; set; }
        public List<Vehicles> _VehicleList = new List<Vehicles>();
        public VehicleClass()
        {
            _VehicleList.Add(new Vehicles
            {
                id = 001,
                make = "Renault",
                type = "Saloon",
                taxBand = 5,
                created = new DateTime(2015,1,1),
                updated = new DateTime(2015,3,1),
                deleted = DateTime.Now,
                isDeleted = true,
                price = 3000,
                description = "A very comfortable car to ride"
            });
            _VehicleList.Add(new Vehicles
            {
                id = 002,
                make = "Toyota",
                type = "Hatchback",
                taxBand = 2,
                created = new DateTime(2015,2,1),
                updated = new DateTime(2015,3,9),
                deleted = DateTime.Now,
                isDeleted = true,
                price = 2500,
                description = "Reliable, strong, fuel efficient"
            });
            _VehicleList.Add(new Vehicles
            {
                id = 003,
                make = "Audi",
                type= "Saloon",
                taxBand = 6,
                created = new DateTime(2015,4,3),
                updated = new DateTime(2015,6,1),
                deleted = DateTime.Now,
                isDeleted = true,
                price = 6000,
                description = "A high performance car"
            });
        }
    }
}

控制器类别:HomeController.cs

using LogViewer.Classes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace LogViewer.Controllers
{
    public class HomeController : Controller
    {
        // GET: Home
        public ActionResult Index(string sortOrder)
        {
            VehicleClass _vehicles = new VehicleClass();
            ViewBag.IdSortParam = String.IsNullOrEmpty(sortOrder) ? "id_desc" : "";
            ViewBag.MakeSortParam = sortOrder == "Make" ? "make_desc" : "Make";
            switch(sortOrder)
            {
                case "id_desc":
                    _vehicles._VehicleList.OrderByDescending(v => v.id).ToList();
                    break;
                case "make_desc":
                    _vehicles._VehicleList.OrderByDescending(v => v.id).ToList();
                    break;
                default:
                    break;
            }
            return View(_vehicles._VehicleList.ToList());
        }
    }
}

最后我的观点:Index.chtml

@model LogViewer.Classes.VehicleClass
@{
    ViewBag.Title = "Index";
}
<h2>Index</h2>
<table>
    <thead>
        <tr>
            <th>
                @Html.ActionLink("ID", "Index", new { sortOrder = ViewBag.IdSortParam, currentFilter = ViewBag.CurrentFilter})
            </th>
            <th>
                @Html.ActionLink("Make", "Index", new { sortOrder = ViewBag.MakeSortParam, currentFilter = ViewBag.CurrentFilter})
            </th>
            <th>Type</th>
            <th>Tax Band</th>
            <th>Created</th>
            <th>Updated</th>
            <th>Deleted</th>
            <th>Is Deleted</th>
            <th>Price</th>
            <th>Description</th>
        </tr>
    </thead>
</table>
@foreach (var item in Model._VehicleList)
{
<table>
    <tbody>
        <tr>
            <td>@item.id</td>
            <td>@item.make</td>
            <td>@item.type</td>
            <td>@item.taxBand</td>
            <td>@item.created</td>
            <td>@item.updated</td>
            <td>@item.deleted</td>
            <td>@item.isDeleted</td>
            <td>@item.price</td>
            <td>@item.description</td>
        </tr>
    </tbody>
</table>
}

我收到的错误是:

传递到字典中的模型项的类型为"System.Collections.Generic.List`1[LogViewer.Models.Vehicle]",但此字典需要类型为"LogViewer.Classes.VehicleClass"的模型项。

这本字典需要类型为的模型项

因为您的视图中有这个

@model LogViewer.Classes.VehicleClass

您应该将VehicleClass的实例而不是List<Vehicles>传递到您的视图,因此在您的控制器中更改它

return View(_vehicles._VehicleList.ToList());

到这个

return View(_vehicles);

编辑

根据问题评论,你说排序不起作用。这是因为您没有在控制器中进行任何排序。请查看控制器中的以下代码行

switch(sortOrder)
{
    case "id_desc":
        _vehicles._VehicleList.OrderByDescending(v => v.id).ToList();
        break;
    case "make_desc":
        _vehicles._VehicleList.OrderByDescending(v => v.id).ToList();
        break;
    default:
        break;
}

进行

_vehicles._VehicleList.OrderByDescending(v => v.id).ToList(); 

不会更改_vehicles._VehicleList中项目的顺序。您需要像这个一样将订购的列表分配回_vehicles._VehicleList

_vehicles._VehicleList = _vehicles._VehicleList.OrderByDescending(v => v.id).ToList();

此外,如果sortOrder的值是"make_desc",则项目应该按不同的属性排序。我想属性是make,所以你的控制器应该修改为

public ActionResult Index(string sortOrder)
{
    VehicleClass _vehicles = new VehicleClass();
    ViewBag.IdSortParam = String.IsNullOrEmpty(sortOrder) ? "id_desc" : "";
    ViewBag.MakeSortParam = sortOrder == "Make" ? "make_desc" : "Make";
    switch(sortOrder)
    {
        case "id_desc":
            _vehicles._VehicleList = _vehicles._VehicleList.OrderByDescending(v => v.id).ToList();
            break;
        case "make_desc":
            _vehicles._VehicleList = _vehicles._VehicleList.OrderByDescending(v => v.make).ToList();
            break;
        default:
            break;
    }
    return View(_vehicles);
}

将视图(cshtml文件)更改为->

@model List<LogViewer.Classes.VehicleClass>

Index.cshtml中,将您的模型替换为:

@model IEnumerable<LogViewer.Classes.VehicleClass>

您返回的_vehicles._VehicleList.ToList()模型是一个列表,而您的View只接受该类的一个实例。

编辑

您的<table>应在Index.cshtmlforeach循环之外:

此:

    </tr>
    </thead>
</table>
@foreach (var item in Model._VehicleList)
{
<table>
    <tbody>
        <tr>

应切换到此:

       </tr>
    </thead>
    <tbody>
@foreach (var item in Model._VehicleList)
{
         <tr>