0
$\begingroup$

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:

Details.cshtml

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>

 
$\endgroup$

1 Answer 1

1
$\begingroup$

Try this:

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.

$\endgroup$
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you Abdullah. I have edited your first code to my Model that the Details.cshtml feeds from. I've edited the second code into the Details sheet. It runs fine without errors but nothing is showing in the Title, Forename and Surname. You mention to bind the values in the model. I don't understand that - isn't that what the first first screenshot is doing? Thank you.
model.ChiefInvestigator is null, so Title, Forename, and Surname are not available to display. What I meant from binding is this: Maybe in your Detail Controller, I am not sure how you are handling it but if you are using something that includes Table.Find(id); you need to replace it with this this: var model = _context.YourTableName .Include(x => x.ChiefInvestigator) .Include(x => x.PrincipalInvestigator) .FirstOrDefault(x => x.TablePrimaryKey == id); this way, Entity Framework will know that you are trying to retrieve the record. (by binding it).
Thank you again Abdullah. I now have it working. Fantastic!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.