I wonder if you can help me discover how to a display a related model variable other than the related table's primary key (in this case the pk is a id and it is not very helpful).
Model:
[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);
}
}
}
Controller:
public ViewResult Index()
{
return View(db.Plants.ToList());
}
View:
@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>
My guess has been that I should just be able to add 'item.PlantSoilpHs.Name' but that doesn't seem to be the case. Any thoughts would be greatly appreciated!
-Scott
From what I can gather by analyzing your question and comments, you are using the View to return a list of Plant objects - which by your description only contain PlantID and PlantName. You can't return the SoilpHName this way. In order to get the SolpHName associated with a plant, instead of
return View(db.Plants.ToList());
you have to use
return View(db.PlantSoilpHs.ToList());
in the Controller and
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Plant.PlantName)
</td>
<td>
@Html.DisplayFor(modelItem => item.SoilpH.Name)
</td>
</tr>
Forgive me if the code is not accurate, it wasn't tested, and was provided only based on your problem description.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.