I am using ASP.NET MVC with a SQL database backend. I have a table that lists individuals which I use for two fields in another table. One field is for the Chief Investigator (ChiefInvestigatorID), the other for the Principal Investigator (PrincipalInvestID). The same person can be in both fields.
When I use the dropdown list, it does correctly store the ID in the respective fields but the Title, Forename and Surname for each respective ID is not correctly showing. This screenshot shows the Principal Invest ID name appearing for the Chief Invest ID, too.
Here is the dropdown code in the controller:
var resname = _context.LocalResname?
.Select(s => new
{
Text = s.PersonID + " " + s.Title + " " + s.Forename + " " + s.Surname,
Value = s.PersonID,
})
.ToList();
ViewBag.ResNameList = new SelectList(resname, "Value", "Text");
This is the Details.cshtml page:
Here is a snippet of the Details.cshtml sheet code. I currently have the same 'lookup' of Title, Forename and Surname as shown here for the Principal Invest. How can I change this to ensure that both names are correctly displayed? Thank you.
<div class="row">
<div class="col-sm-2">
<div class="form-group">
<br />
<b> @Html.DisplayNameFor(model => model.ChiefInvestigatorID) </b>
<div class = "col-sm-10">
@Html.DisplayFor(model => model.ChiefInvestigatorID)
</div>
</div>
</div>
<div class="col-sm-2">
<div class="form-group">
<br />
<b> @Html.DisplayNameFor(model => model.LocalResname.Title) </b>
<div class="col-sm-10">
@Html.DisplayFor(model => model.LocalResname.Title)
</div>
</div>
</div>
<div class="col-sm-2">
<div class="form-group">
<br />
<b> @Html.DisplayNameFor(model => model.LocalResname.Forename) </b>
<div class="col-sm-10">
@Html.DisplayFor(model => model.LocalResname.Forename)
</div>
</div>
</div>
<div class="col-sm-2">
<div class="form-group">
<br />
<b> @Html.DisplayNameFor(model => model.LocalResname.Surname) </b>
<div class="col-sm-10">
@Html.DisplayFor(model => model.LocalResname.Surname)
</div>
</div>
</div>
I guess the issue is with the Route. You need to add another navigation property.
Currently both of your fields are referenced to one point which is LocalResname.
If you try something like creating a new Navigation, then it will be no more ambiguous.
In your controller, you can add 2 foreign keys like this
public int ChiefInvestigatorID { get; set; }
public int PrincipalInvestID { get; set; }
[ForeignKey("ChiefInvestigatorID")]
public virtual LocalResname ChiefInvestigator { get; set; }
[ForeignKey("PrincipalInvestID")]
public virtual LocalResname PrincipalInvestigator { get; set; }
then try this below:
<div class="row">
<div class="col-sm-3">
<div class="form-group">
<b>Chief Investigator</b>
<div>
@Html.DisplayFor(model => model.ChiefInvestigator.Title)
@Html.DisplayFor(model => model.ChiefInvestigator.Forename)
@Html.DisplayFor(model => model.ChiefInvestigator.Surname)
</div>
</div>
</div>
<div class="col-sm-3">
<div class="form-group">
<b>Principal Investigator</b>
<div>
@Html.DisplayFor(model => model.PrincipalInvestigator.Title)
@Html.DisplayFor(model => model.PrincipalInvestigator.Forename)
@Html.DisplayFor(model => model.PrincipalInvestigator.Surname)
</div>
</div>
</div>
</div>
Also you need to bind these values in your model. Include both of these when retrieving the model.