Dictionary需要IEnumerable类型的模型项

时间:2017-06-22 20:54:51

标签: asp.net-mvc entity-framework linq

我是Linq的MVC新手,在我的项目中,我收到了一个错误:

  

传递到字典中的模型项是'System.Boolean'类型,但是这个字典需要一个类型为'System.Collections.Generic.IEnumerable`1 [Parts.PartsLocation]'的模型项。

我在EF中使用MVC,那么如何返回IEnumerable?

这是我的控制器:

  public async Task<ActionResult> Index(String aContainer)
  {
      var container = from x in db.PartsLocations select x;
      var empty = from y in db.PartsLocations select y;

      if (!String.IsNullOrEmpty(aContainer))
      {
          var parent = from a in db.PartsLocations
                       where (a.LocationName == aContainer)
                       select a.ParentLocation;
          return View(await parent.ToListAsync());
      }

      empty = empty.Where(r => r.LocationName.Contains(null));
      return View(await empty.ToListAsync());
  }  

查看:

@model IEnumerable<Parts.PartsLocation>

@{
    ViewBag.Title = "Index";
}

型号:

namespace Parts
{
using System;
using System.Collections.Generic;

public partial class PartsLocation
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public PartsLocation()
    {
        this.ManufacturingParts = new HashSet<ManufacturingPart>();
    }

    public int LocationID { get; set; }
    public int TypeID { get; set; }
    public string LocationName { get; set; }
    public string Description { get; set; }
    public int PlantID { get; set; }
    public Nullable<int> BayID { get; set; }
    public Nullable<int> SecurityGroupID { get; set; }
    public Nullable<int> ParentLocation { get; set; }
    public Nullable<System.DateTime> LastMoved { get; set; }
    public string Notes { get; set; }

    public virtual LocationType LocationType { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<ManufacturingPart> ManufacturingParts { get; set; }
    public virtual ManufacturingPlant ManufacturingPlant { get; set; }
}
}

1 个答案:

答案 0 :(得分:-1)

aContainer not Null然后你返回选择

      a.ParentLocation
      else PartsLocation List 

视图模型返回差异,因此验证返回值