learning to work around c# and razor. I have this issue which I am struggling to get my head around. I have researched this forum to understand but to no avail. The model item is of type CookMeIndexViewModel, but requires a model item of type IEnumerable<CookMeIndexViewModel>
Anyway this is the code I have been working with: However my data are coming from my VehicleClass. My 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"
});
}
}
}
Controller Class: 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());
}
}
}
Finally my View: 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>
}
The error I have been receiving is this:
The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[LogViewer.Models.Vehicles]', but this dictionary requires a model item of type 'LogViewer.Classes.VehicleClass'.
return View(_vehicles);
in yourIndex
action – Mike Debela