2012-01-10 47 views
0

我不知道您是否可以幫助我發現如何顯示除相關表主鍵之外的相關模型變量(在本例中,pk是一個id並且這不是很有幫助)。顯示除相關表主鍵(ID)以外的相關變量

型號:

[EdmRelationshipNavigationPropertyAttribute("PlantModel", "FK_PlantSoilpH_Plant", "PlantSoilpH")] 
     public EntityCollection<PlantSoilpH> PlantSoilpHs 
     { 
      get 
      { 
       return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedCollection<PlantSoilpH>("PlantModel.FK_PlantSoilpH_Plant", "PlantSoilpH"); 
      } 
      set 
      { 
       if ((value != null)) 
       { 
        ((IEntityWithRelationships)this).RelationshipManager.InitializeRelatedCollection<PlantSoilpH>("PlantModel.FK_PlantSoilpH_Plant", "PlantSoilpH", value); 
       } 
      } 
     } 

控制器:

public ViewResult Index() 
{ 
    return View(db.Plants.ToList()); 
} 

查看:

@foreach (var item in Model) { 
    <tr> 
     <td> 
      @Html.DisplayFor(modelItem => item.PlantName) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.ScientificName) 
     </td> 
     <td> 
      @Html.DisplayFor(modelItem => item.PlantSoilpHs) 
     </td> 

我的猜測一直認爲我應該能夠添加 'item.PlantSoilpHs.Name'但似乎並非如此。任何想法將不勝感激!

斯科特

+0

'Plant.PlantSoilpHs'是一個集合,不是嗎?你想展示什麼?一個名爲「PlantSoilpH」或集合中第一個元素名稱的「嵌套」表或? – Slauma 2012-01-10 18:08:20

+0

我在數據庫中有3個表格:Plant,PlantSoilpH和SoilpH。 Plant是一個包含PlantID(PK)和PlantName的表格。 SoilpH是一個表,其中包含:SoilpHID(PK)和SoilpHName。 PlantSoilpH是一個包含PlantSoilpHID(PK),PlantID(FK/1:1)和SoilpHID(FK/Many:1)的表格。我對收藏品的瞭解不夠多,不能回答你的問題,但我想要做的是展示Plant's SoilpHName。我得到的是PlantSoilpHID。 Noob在這裏。謝謝你的幫助! – skhot 2012-01-10 20:07:26

+0

我只能猜測:'@ Html.DisplayFor(modelItem => item.PlantSoilpHs.Select(p => p.SoilpH.Name))'。 – Slauma 2012-01-10 23:55:21

回答

0

從我可以通過分析你的問題和意見收集,您正在使用的視圖返回工廠對象的列表 - 它通過你的描述只包含PlantID和PlantName。你不能以這種方式返回SoilpHName。爲了得到與工廠相關的SolpHName,而不是

return View(db.Plants.ToList()); 

,你必須使用

return View(db.PlantSoilpHs.ToList()); 
在控制器

@foreach (var item in Model) { 
<tr> 
    <td> 
     @Html.DisplayFor(modelItem => item.Plant.PlantName) 
    </td> 
    <td> 
     @Html.DisplayFor(modelItem => item.SoilpH.Name) 
    </td> 
</tr> 

原諒我,如果代碼是不準確,它沒有經過測試,只是根據您的問題描述提供。