简体   繁体   中英

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

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

Your View is expecting a type DeviceLogIndex however you are passing it a type List<DeviceLog>

What you need to do instead is assign the List<DeviceLog> to the DeviceLogIndex

For Example:

DeviceLogIndex model = new DeviceLogIndex();
model.DeviceLogs = youDeviceLoglist;
return View(model);

Then in your View:

@foreach (var dl in Model.DeviceLogs)
{
    //Do something
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM