2017-06-22 87 views
1

我是新來的MVC與LINQ和在我的項目,我得到一個錯誤:字典需要的IEnumerable類型的模型項目

The model item passed into the dictionary is of type 'System.Boolean', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[Parts.PartsLocation]'.

我使用MVC與EF,所以我怎麼返回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; } 
} 
} 
+0

您可以發佈您的型號代碼嗎? –

+0

對不起,我現在編輯模型。 –

+0

這是發生在傳入的字符串爲空/空的情況下嗎?這應該返回List ,它將滿足IEnumerable。突出的一件事是empty = empty.Where(r => r.LocationName.Contains(null));這看起來應該是empty = empty.Where(r => null == r.LocationName); –

回答

-1

的aContainer不等於零時,你必須選擇回報

 a.ParentLocation 
     else PartsLocation List 

視圖模型返回差異如此驗證返回值

+0

嗨,我不知道「拖回是如此驗證返回值」是什麼意思。你能解釋一下嗎? –

相關問題