How do I get an Asp.Net drop down box to show the list of applicant types instead of the list of ApplicantTypeId primary keys with Entity Framework

Babak Sekandari
Babak Sekandari used Ask the Experts™
on
I have an ASP.NET MVC application built database first and Entity Framework.
The Sql Server database has a table named Applicant that has a foreign key to a reference table called ApplicantType_Ref.
When I create the scaffolding, the resulting model, Applicants.cs has the foreign key, ApplicantTypeId as a property:
public int? ApplicantTypeId { get; set; }

Open in new window

When I run the application and create a new applicant, the resulting view shows me, as expected, a drop down for the Applicant Type Id.
What I need to see instead is a drop down for the Applicant Type in the same row of the table as the Applicant Type Id.
How do I do that?
Do I change something in the Applicant.cs model before creating the controller?
I do see a property for the ApplicantType_Ref table itself:
public ApplicantTypeRef ApplicantType { get; set; }

Open in new window

Do I use that property in the view or the controller, replacing the other property, ?
In other words, how do I get the drop down box to show the actual applicant type instead of the list of ApplicantTypeId primary keys?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Dropdownlist is my least favorite control :)

You need to do something like this:

https://odetocode.com/blogs/scott/archive/2013/03/11/dropdownlistfor-with-asp-net-mvc.aspx
Babak SekandariProgrammer / Analyst

Author

Commented:
I don't understand how that applies to the issue I'm trying to solve. I'm not asking how to use the control. I'm asking about how to get the data.
I have a model that represents the Applicant table. The applicant table has a foreign key to a reference table. All I'm getting back are the primary keys of the reference table. I'm trying to get back the values, not the primary keys. Is there some sort of Linq statement that I put in the model?
Am I supposed to use a ViewModel when one table references another table?
You have to join  the tables ... get the  Id and description field together  and then use that link to bind to the dropdown.
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

And yes, you can use linq to join the 2 tables.
Babak SekandariProgrammer / Analyst

Author

Commented:
Thank you. Is that typically done in the model? Like in the Applicant.cs class?
Perhaps I'm supposed to create a ViewModel when there are two tables?
Is it done in the .cshtml by joining two different entities?
Is it done in the controller?
I'm very new to MVC.
Thanks.
I do it in the controller. I don't have my mvc code in front of me. If I find an example, I'll post back.
Here, this is a good example

Example
Babak SekandariProgrammer / Analyst

Author

Commented:
Ok. I'm going to play around with that example and get back to comment what I found.
I also found an example here on how to accomplish this in the model: Primary key and Foreign key relationship in Model in ASP.NET MVC
The problem with that example is that it might be doing a code-first approach. My scaffolding came from a database first approach.
Also, my model, Applicant.cs already contains a relationship :
public ApplicantTypeRef ApplicantType { get; set; }
I was hoping there was some way I could leverage that.
Babak SekandariProgrammer / Analyst

Author

Commented:
I changed my Applicant.cs model to:

// Foreign key
        [Display(Name = "ApplicantTypeRef")]
        public int ApplicantTypeId { get; set; }

        [ForeignKey("ApplicantTypeId")]
        public virtual ApplicantTypeRef ApplicantType { get; set; }

Then I changed my Create.cshtml to:
<div class="form-group">
    <label asp-for="ApplicantType" class="control-label"></label>
    <select asp-for="ApplicantType" class="form-control" asp-items="ViewBag.ApplicantType"></select>
</div>

However, when I try to create a new Applicant and I click on the Applicant Type drop down box, it is blank.
Any suggestions?
what do you have in ViewBag.ApplicantType?
  <select asp-for="ApplicantType" class="form-control" asp-items="ViewBag.ApplicantType"></select>

Look at the link I posted above to build a dropdown.
Babak SekandariProgrammer / Analyst

Author

Commented:
Thank you Camillia.
I didn't recognize the example you offered as correct because I didn't know enough about Asp.Net MVC at that time.

There are still somethings, however, that I don't understand.
In my Controller class, I have this:
// GET: Applicants/Create
public IActionResult Create()
{
    ViewData["ApplicantType"] = new SelectList(_context.ApplicantTypeRef, "ApplicatTypeId", "ApplicantType");
    ViewData["ExemptionId"] = new SelectList(_context.FlsaExemptionRef, "ExemptionId", "Exemption");          
    return View();            
}

In my .cshtml View, I have this:
<div class="form-group">
    <label asp-for="ExemptionId" class="control-label"></label>
    <select asp-for="ExemptionId" class="form-control" asp-items="ViewBag.ExemptionId"></select>
</div>
<div class="form-group">
    <label asp-for="ApplicantType" class="control-label"></label>
    <select asp-for="ApplicantType" class="form-control" asp-items="ViewBag.ApplicantType"></select>
</div>  

In my Applicant.cs model, I have this:
// Foreign key
[Display(Name = "ApplicantTypeRef")]
public int ApplicantTypeId { get; set; }
[ForeignKey("ApplicantTypeId")]
public virtual ApplicantTypeRef ApplicantType { get; set; }

public FlsaExemptionRef Exemption { get; set; }

Both ways work. That is, both the Exemption and the ApplicantType work for the controller.
The Exemption was the default way that the Entity Control scaffolding setup.
The ApplicantType was what I manually setup.

My question is, is there an advantage or disadvantage to one way versus the other. If not, I can just use what EF sets up.
Thanks.
I don't know the answer to that. If you're using EF , be consistent and use that.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial