传递到字典中的模型项的类型为&System; Collection.Collections.Generic.List`

时间:2016-08-03 15:40:33

标签: c# asp.net-mvc

我正在尝试在我的索引视图中进行分页和排序,而不使用PagedList。但是我遇到了错误 -

  

传递到字典中的模型项的类型为System.Collections.Generic.List`1 [AQB_MON.Models.DeviceLog]',但此字典需要类型为' AQB_MON.ViewModels的模型项.DeviceLogIndex'

我创建了自己的ViewModel来将参数传递给Create View。当我进入DeviceLog索引视图时,它是针对特定的DeviceID,我想将此DeviceID传递给Create View,但是我在使用PagedList时遇到了一些问题。所以我找到了另一种选择,但遇到了上述错误。

我的DeviceLogIndex ViewModel由 -

组成
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; }

和用于分页和排序的PageInfo ViewModel是 -

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

我的观点的第一部分是 -

@model AQB_MON.ViewModels.DeviceLogIndex

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

我在想&#39;信息&#39;声明导致问题?

我的控制器是 -

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

2 个答案:

答案 0 :(得分:2)

您的视图是DeviceLogIndex的强类型,但是从您的操作方法中,您将List<DeviceLog>传递给视图。这就是你得到不匹配错误的原因。

要解决此问题,您应该创建DeviceLogIndex类的对象,并从LINQ查询结果中分配DeviceLogs属性值。并将DeviceLogIndex对象传递给视图。

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

您可以在视图中访问Model.DeviceLogs以访问每个设备项目。

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

答案 1 :(得分:2)

您的视图需要DeviceLogIndex类型但是您传递的类型为List<DeviceLog>

您需要做的是将List<DeviceLog>分配给DeviceLogIndex

例如:

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

然后在你的视图中:

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