我想知道你是否可以帮我发现如何显示除相关表的主键之外的相关模型变量(在这种情况下,pk是一个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',但似乎并非如此。任何想法都将不胜感激!
-Scott
答案 0 :(得分:0)
通过分析您的问题和评论,我可以使用View来返回Plant对象列表 - 您的描述中只包含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>
如果代码不准确,未经过测试,并且仅根据您的问题描述提供,请原谅。