Let's say I have an entity named Animal
which has a column called Type
and it is a foreign key to a lookup table. I need a strongly typed relation between the view and Animal
. How do I get the values stored in the lookup table into my view, so I can populate a combobox with the values from Type
.
Asked
Active
Viewed 112 times
-1
-
Does this answer your question? [How to use a ViewBag to create a dropdownlist?](https://stackoverflow.com/questions/16594958/how-to-use-a-viewbag-to-create-a-dropdownlist) – cwalvoort Dec 24 '19 at 21:14
1 Answers
0
Operating with DB entity in the view is not the best approach from architectural point of view. It will be better if you create a separate ViewModel for that and use it in your view. For example, here is your DB entities:
public class Animal
{
public int Type { get; set; }
}
public class AnimalType
{
public int Type { get; set; }
public string Name { get; set; }
}
And here is your ViewModel:
public class AnimalViewModel
{
public IDictionary<int, string> Animals { get; set; }
}
So in controller/service you can get your Animals and their type names (using Entity Framework navigation property or direct JOIN), create your ViewModel and pass to the view.

Roman.Pavelko
- 1,555
- 2
- 15
- 18