how can i get the data from database and put them to a dropdown
how can i get the data from database and put them to a dropdown , when im trying to do that it shows me in dropdown some rows with this : Microsoft.Aspnetcore.mvc.rendering.selectlistitem
63 Replies
i have 6 rows that it should displayed but instead of those names it shows 6 records with : Microsoft.Aspnetcore.mvc.rendering.selectlistitem
How are you displaying them now?
Or just ask a question and dip, sure, that's also fine
ye wait
i have a method in my controller :
and i want to display them in a dropdown list
i get GetKategorite() method from my IPunetRepository
then i implement this method to PunetRepository:
now i want to display them in my view
but the names doesnt show up :
Well, if you want a select list, you need a list of
SelectListItem
s, then you can use it with asp-items
on a <select>
element
Also, don't use ViewBag
so instead of ToList method to switch to SelectListItem
oh okay
from SelectList
what to use instead of
Example
Pass it to the
View()
where to get that Id
It's an example
cuz im not going to get that data from a foreign key
ah okay
Ideally, since you store the categories in the database, they would have a primnary key or some other kind of an ID
So you would use that
![No description](https://cdn.answeroverflow.com/1201288563984187462/image.png)
its showing those 6 records
the category attribute in my database is stored as a string
i have 6 different categories
It looks like you're still creating the select list manually instead of using
asp-items
That, or you're generating list items incorrectly
So, show the piece of code where you generate the list items, and show the piece of code where you display the <select>
list