这本词典需要一个类型的模型项

时间:2015-06-29 11:16:24

标签: c# asp.net-mvc

学习围绕c#和剃刀工作。我有这个问题,我正在努力解决这个问题。 我已经研究过这个论坛了解但无济于事。 The model item is of type CookMeIndexViewModel, but requires a model item of type IEnumerable<CookMeIndexViewModel>

无论如何这是我一直在使用的代码: 但是我的数据来自我的VehicleClass。 我的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.cshtml

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

我收到的错误是:

  

传递到字典中的模型项的类型为&#39; System.Collections.Generic.List`1 [LogViewer.Models.Vehicles]&#39;,但此字典需要类型为&#39的模型项; LogViewer.Classes.VehicleClass&#39;

3 个答案:

答案 0 :(得分:0)

将您的观点(cshtml文件)更改为&gt;

@model List<LogViewer.Classes.VehicleClass>

答案 1 :(得分:0)

Index.cshtml替换你的模型:

@model IEnumerable<LogViewer.Classes.VehicleClass>

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

修改

您的&lt; table&gt;应该在Index.cshtml foreach循环之外:

此:

    </tr>
    </thead>
</table>

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

应切换到:

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

答案 2 :(得分:0)

因为你在视图中有这个

@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);
}