Ethel Patrick Ethel Patrick - 4 months ago 90
C# Question

The model item passed into the dictionary is of type 'System.Collections.Generic.List`

I am trying to do paging and sorting in my Index view without using PagedList. However I am encountering the error -


The model item passed into the dictionary is of type System.Collections.Generic.List`1[AQB_MON.Models.DeviceLog]', but this dictionary requires a model item of type 'AQB_MON.ViewModels.DeviceLogIndex'.


I have created my own ViewModel to pass parameters to the Create View. When I enter the DeviceLog Index View it is for a specific DeviceID and I want to pass this DeviceID to the Create View however I encountered some issues using PagedList. So I found an alternative however I encounter the above error.

My DeviceLogIndex ViewModel consists of -

public class DeviceLogIndex
{
public DeviceLogIndex()
{
this.DeviceLogs = new List<DeviceLog>();
}
public int DeviceID { get; set; }
public string mytest { get; set; }
public List<DeviceLog> DeviceLogs { get; set; }


and my PageInfo ViewModel that is used for Paging and sorting is -

public class PagingInfo
{
public string SortField { get; set; }
public string SortDirection { get; set; }
public int PageSize { get; set; }
public int PageCount { get; set; }
public int CurrentPageIndex { get; set; }
}


the first part of my view is -

@model AQB_MON.ViewModels.DeviceLogIndex

@{
ViewBag.Title = "Index";
}
@{
AQB_MON.ViewModels.PagingInfo info = ViewBag.PagingInfo;
}


I am thinking the 'info' declaration is causing the problem?

My controller is -

public ActionResult Index(int id)
{
var devicelogs = db.DeviceLogs
.Include(d => d.Device)
.Include(d => d.DeviceLogType)
.Include(d => d.Device.ManufacturerModel)
.Where(d => d.DeviceID == id);
{
PagingInfo info = new PagingInfo();
info.SortField = "DeviceLog";
info.SortDirection = "ascending";
info.PageSize = 15;
info.PageCount = Convert.ToInt32(Math.Ceiling((double)(db.DeviceLogs.Count()/info.PageSize)));
info.PageCount +=1;
info.CurrentPageIndex = 0;
var query = devicelogs.OrderBy(c => c.DeviceLogID).Take(info.PageSize);
ViewBag.PagingInfo = info;
List<DeviceLog> model= query.ToList();

return View(model);
}
}

[HttpPost]
public ActionResult Index(PagingInfo info, int id)
{
var devicelogs = db.DeviceLogs
.Include(d => d.Device)
.Include(d => d.DeviceLogType)
.Include(d => d.Device.ManufacturerModel)
.Where(d => d.DeviceID == id);
{
IQueryable<DeviceLog> query = null;
switch(info.SortField)
{
case "EntryDate":
query = (info.SortDirection == "ascending" ?
devicelogs.OrderBy(c => c.EntryDate) :
devicelogs.OrderByDescending(c => c.EntryDate));
break;
case "LogType":
query = (info.SortDirection == "ascending" ?
devicelogs.OrderBy(c => c.DeviceLogType) :
devicelogs.OrderByDescending(c => c.DeviceLogType));
break;
}

query = query.Skip(info.CurrentPageIndex * info.PageSize).Take(info.PageSize);
ViewBag.PagingInfo = info;
List<DeviceLog> model = query.ToList();
return View(model);
}
}

Answer

Your view is strongly typed to DeviceLogIndex, but from your action method you are passing a List<DeviceLog> to the view. That is the reason you are getting the mismatch error.

To solve the issue, you should create an object of your DeviceLogIndex class and assign the DeviceLogs property value from your LINQ query result. and pass the DeviceLogIndex object to the view.

DeviceLogIndex vm = new DeviceLogIndex();

//your existing code goes here

var query = devicelogs.OrderBy(c => c.DeviceLogID).Take(info.PageSize);
vm.DeviceLogs=query.ToList();
return View(vm);

You can access Model.DeviceLogs in your view to access each Device item.

@model AQB_MON.ViewModels.DeviceLogIndex
@foreach(var item in Model.DeviceLogs)
{
  <p>@item.DeviceLogID</p>
}
Comments