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
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>
listSo, yes, I was correct
but when i try to switch to
asp-items
it shows me an error
i have this model to my index.cstml
@model IEnumerable<Punet>
and Punet model :
And I just told you how to pass your list of items via the model, instead of using the viewbag
first what to write at asp-items
i try
So:
1. Get your punets
2. Get your list items
3. Stick your punets and list items into a single object
4. Pass that object as the model
Then
asp-items="@Model.ListItems"
now its displaying the categories
but i dont want to see the others Microsoft.aspnetcore...
Don't display the options manually, then
okay
thank u
it worked
but now when im selecting a category
so jobs means "punet" to my language
nothing happens
i want that jobs with selected categories to be displayed
Well, you will have to handle that somehow
if i choose "Teknologji"
that display only two
You'll get the category ID, or whatever else you've chosen as the key, in a
kategoria
property of the request, since that's the name
of your select list
So filter the jobs by thatit seems hard
No, it doesn't, really
so i need to create a new method to my controller
You can use the same method, sure
Just make the parameter nullable
so when i try
the Punet doesnt have this method
Well, no wonder, seeing how you're using the generic repository pattern for some reason
Add a
GetPunetsByCategory()
method to your repository, for example
And filter thereokay
int GetPunetByCategory();
is this ok
then i implement this to PunetRepository
int GetPunetByCategory(int category);
You only want to get an integer out of it?
no
I thought you wanted a list of punets in a given category
Adjust accordingly, then
so IEnumerable<string> GetPunetByCategory?
You only want to get the strings?
Like, only punet names?
Or whole, entire punets?
actually i want all the props for that job
as displayed
Adjust accordingly, then
so this may be correct : IEnumerable<Punet> GetPunetByCategory();
Almost: how would that method know what category you are looking for?
Yes, that would be the correct return type
Although I'd use
List
instead of IEnumerable
if you've already got a list anyway, saves another ToList()
call down the line most of the time.okay let me try as a list
now how to implement
public List<Punet> GetPunetByCategory()
{
return _db.Punet.Where(j=>j.kategoria)
}
shouldi pass
Well, you need to give this method something to filter by
a param to the method
It would be useful, yes
Otherwise, what will you filter by?
nothing haha
so string kategoria ?
or Punet p
public List<Punet> GetPunetByCategory(Punet p)
{
return _db.Punet.Where(j =>j.kategoria == p.kategoria);
}
Uh
I thought you want to sort by a categoiry
Not by a punet
Why are you passing a punet as a parameter?
Pass category ID, category name, something like that
ID, preferably
public List<Punet> GetPunetByCategory(int categoryId)
{
return _db.Punet.Where(j =>j.kategoria == categoryId);
}
Is
j.kategoria
an int?no
as a string
so maybe its good
that
string kategori
You cannot compare it to an int, then
to the params
That would work, yes
Although, in general, I would recommend you make use of the fact you're using a relational database
And the punet should have a relationship to a category
Instead of storing its name as a string
yes in fact
that would be good
but i had some problems
with foreign key
Should've fixed that
Anyway
You need to resolve the query
.Where()
creates an IQueryable
If you want to resolve it, you need .ToList()
, .ToListasync()
, .ToArray()
, .ToArrayAsync()
, etc
One of those
Ideally, the async variantits not working
idk
thank you anyway