2016-07-07 82 views
1

当我编译我的节目,我有这样的错误:错误时显示视图

'System.Collections.Generic.ICollection' does not contain a definition for 'WIE_Ilosc' and no extension method 'WIE_Ilosc' accepting a first argument of type 'System.Collections.Generic.ICollection' could be found (are you missing a using directive or an assembly reference?)

我必须在我的代码改变什么,使其正常工作?

我的观点:

@model List<Webb.Models.Faktury> 
@{ 
    Layout = null; 
} 
<!DOCTYPE html> 
<html> 
<head> 
</head> 
<body> 
    <h2>Faktura VAT</h2> 
    <p> 
     Oryginal</p> 
    <table width="100%"> 
     <tr> 
      <td>ID</td> 
      <td>Data S.</td> 
      <td>Numer</td> 
     </tr> 
     @foreach (var item in Model) 
     { 
      <tr> 
       <td>@item.FAK_Id</td> 
       <td>@item.FAK_DataS</td> 
       <td>@item.Firma.FIR_Rachunek</td> 
       <td>@item.Wierszes.WIE_Ilosc</td> 
      </tr> 
     } 
    </table> 

</body> 
</html> 

我的控制器:模型的

public ActionResult Reports(int? id) 
     { 
      // Setup sample model 
      var pro = (from a in db.Fakturies 
         join b in db.Wierszes on a.FAK_Id equals b.WIE_Fkid 
         join c in db.Produkties on b.WIE_Pid equals c.PRO_Id 
         select a); 

      pro = pro.Where(a => a.FAK_Id == id); 

      if (Request.QueryString["format"] == "pdf") 
       return new PdfResult(pro.ToList(), "Reports"); 

      return View(pro); 
     } 

部分:

public Faktury() 
     { 
      this.Wierszes = new HashSet<Wiersze>(); 
     } 
    . 
    . 
    . 
    . 

     public virtual ICollection<Wiersze> Wierszes { get; set; } 
     public virtual Firma Firma { get; set; } 
     public virtual Klienci Klienci { get; set; } 
     public virtual Statusy Statusy { get; set; } 
    } 

回答

3

看这句话在你的剃须刀代码。

<td>@item.Wierszes.WIE_Ilosc</td> 

但按你的类定义,Wierszes财产上Faktury类是一个集合类型(ICollection<Wiersze>)。在您看来,您正尝试访问集合上的WIE_Ilosc属性!

如果你想显示所有Wiersze的,你应该循环遍历它们并呈现它。

@foreach (var item in Model) 
{ 
    <tr> 
     <td>@item.FAK_Id</td> 
     <td>@item.FAK_DataS</td> 
     <td> 
      @if(item.Wierszes!=null) 
      { 
       foreach(var v in item.Wierszes) 
       { 
        <span>@v.WIE_Ilosc</span> 
       } 
      } 
     </td> 
    </tr> 
} 
+0

由于Wierszes没有在LINQ查询预计不会'item.Wierszes'总是空 –

+0

Faktury有这个'Wierszes'属性和延迟加载可能给他的数据。有一段时间没有使用EF了:) – Shyju