Babak Sekandari
asked on
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
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:
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:
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?
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; }
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; }
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?
ASKER
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?
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.
And yes, you can use linq to join the 2 tables.
ASKER
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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
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.
ASKER
I changed my Applicant.cs model to:
// Foreign key
[Display(Name = "ApplicantTypeRef")]
public int ApplicantTypeId { get; set; }
[ForeignKey("ApplicantType Id")]
public virtual ApplicantTypeRef ApplicantType { get; set; }
Then I changed my Create.cshtml to:
<div class="form-group">
<label asp-for="ApplicantType" class="control-label"></la bel>
<select asp-for="ApplicantType" class="form-control" asp-items="ViewBag.Applica ntType"></ 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?
// Foreign key
[Display(Name = "ApplicantTypeRef")]
public int ApplicantTypeId { get; set; }
[ForeignKey("ApplicantType
public virtual ApplicantTypeRef ApplicantType { get; set; }
Then I changed my Create.cshtml to:
<div class="form-group">
<label asp-for="ApplicantType" class="control-label"></la
<select asp-for="ApplicantType" class="form-control" asp-items="ViewBag.Applica
</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.Applica ntType"></ select>
Look at the link I posted above to build a dropdown.
<select asp-for="ApplicantType" class="form-control" asp-items="ViewBag.Applica
Look at the link I posted above to build a dropdown.
ASKER
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.Applic antTypeRef , "ApplicatTypeId", "ApplicantType");
ViewData["ExemptionId"] = new SelectList(_context.FlsaEx emptionRef , "ExemptionId", "Exemption");
return View();
}
In my .cshtml View, I have this:
<div class="form-group">
<label asp-for="ExemptionId" class="control-label"></la bel>
<select asp-for="ExemptionId" class="form-control" asp-items="ViewBag.Exempti onId"></se lect>
</div>
<div class="form-group">
<label asp-for="ApplicantType" class="control-label"></la bel>
<select asp-for="ApplicantType" class="form-control" asp-items="ViewBag.Applica ntType"></ select>
</div>
In my Applicant.cs model, I have this:
// Foreign key
[Display(Name = "ApplicantTypeRef")]
public int ApplicantTypeId { get; set; }
[ForeignKey("ApplicantType Id")]
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 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.Applic
ViewData["ExemptionId"] = new SelectList(_context.FlsaEx
return View();
}
In my .cshtml View, I have this:
<div class="form-group">
<label asp-for="ExemptionId" class="control-label"></la
<select asp-for="ExemptionId" class="form-control" asp-items="ViewBag.Exempti
</div>
<div class="form-group">
<label asp-for="ApplicantType" class="control-label"></la
<select asp-for="ApplicantType" class="form-control" asp-items="ViewBag.Applica
</div>
In my Applicant.cs model, I have this:
// Foreign key
[Display(Name = "ApplicantTypeRef")]
public int ApplicantTypeId { get; set; }
[ForeignKey("ApplicantType
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.
You need to do something like this:
https://odetocode.com/blogs/scott/archive/2013/03/11/dropdownlistfor-with-asp-net-mvc.aspx