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'但似乎並非如此。任何想法將不勝感激!
斯科特
'Plant.PlantSoilpHs'是一個集合,不是嗎?你想展示什麼?一個名爲「PlantSoilpH」或集合中第一個元素名稱的「嵌套」表或? – Slauma 2012-01-10 18:08:20
我在數據庫中有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
我只能猜測:'@ Html.DisplayFor(modelItem => item.PlantSoilpHs.Select(p => p.SoilpH.Name))'。 – Slauma 2012-01-10 23:55:21