C
C#10mo ago
ShuajbM

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
ShuajbM
ShuajbMOP10mo ago
i have 6 rows that it should displayed but instead of those names it shows 6 records with : Microsoft.Aspnetcore.mvc.rendering.selectlistitem
Angius
Angius10mo ago
How are you displaying them now? Or just ask a question and dip, sure, that's also fine
ShuajbM
ShuajbMOP10mo ago
ye wait i have a method in my controller :
c#
public IActionResult Index(string searchString,string kategoria)
{

var kategorite = _unitOfWork.Punet.GetKategorite();
ViewBag.Categories = new SelectList(kategorite);

IEnumerable<Punet> punetList = _unitOfWork.Punet.GetAll();

if (!string.IsNullOrEmpty(kategoria))
{
punetList = punetList.Where(p => p.kategoria == kategoria);
}

if (!String.IsNullOrEmpty(searchString))
{
punetList = punetList.Where(p => p.Name.Contains(searchString));
}
else
{
punetList = _unitOfWork.Punet.GetAll();
}

return View(punetList);
}
c#
public IActionResult Index(string searchString,string kategoria)
{

var kategorite = _unitOfWork.Punet.GetKategorite();
ViewBag.Categories = new SelectList(kategorite);

IEnumerable<Punet> punetList = _unitOfWork.Punet.GetAll();

if (!string.IsNullOrEmpty(kategoria))
{
punetList = punetList.Where(p => p.kategoria == kategoria);
}

if (!String.IsNullOrEmpty(searchString))
{
punetList = punetList.Where(p => p.Name.Contains(searchString));
}
else
{
punetList = _unitOfWork.Punet.GetAll();
}

return View(punetList);
}
and i want to display them in a dropdown list i get GetKategorite() method from my IPunetRepository
c#
public interface IPunetRepository: IRepository<Punet>
{
void Update(Punet obj);

IEnumerable<string> GetKategorite();
}
c#
public interface IPunetRepository: IRepository<Punet>
{
void Update(Punet obj);

IEnumerable<string> GetKategorite();
}
then i implement this method to PunetRepository:
c#
public IEnumerable<string> GetKategorite()
{
return _db.Punet.Select(k => k.kategoria).Distinct().ToList();
}
c#
public IEnumerable<string> GetKategorite()
{
return _db.Punet.Select(k => k.kategoria).Distinct().ToList();
}
now i want to display them in my view but the names doesnt show up :
ShuajbM
ShuajbMOP10mo ago
Angius
Angius10mo ago
Well, if you want a select list, you need a list of SelectListItems, then you can use it with asp-items on a <select> element Also, don't use ViewBag
ShuajbM
ShuajbMOP10mo ago
so instead of ToList method to switch to SelectListItem oh okay from SelectList what to use instead of
ViewBag
ViewBag
Angius
Angius10mo ago
Example
public async Task<IActionResult> OnGet()
{
var items = await _context.Things
.Select(thing => new SelectListItem {
Value = thing.Id,
Text = thing.Name
})
.ToListAsync();

var data = new DataDto {
Items = items
};

return View(data);
}
public async Task<IActionResult> OnGet()
{
var items = await _context.Things
.Select(thing => new SelectListItem {
Value = thing.Id,
Text = thing.Name
})
.ToListAsync();

var data = new DataDto {
Items = items
};

return View(data);
}
@model DataDto

<select asp-items="@Model.Items"></select>
@model DataDto

<select asp-items="@Model.Items"></select>
Pass it to the View()
ShuajbM
ShuajbMOP10mo ago
where to get that Id
Angius
Angius10mo ago
It's an example
ShuajbM
ShuajbMOP10mo ago
cuz im not going to get that data from a foreign key ah okay
Angius
Angius10mo ago
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
ShuajbM
ShuajbMOP10mo ago
No description
ShuajbM
ShuajbMOP10mo ago
its showing those 6 records the category attribute in my database is stored as a string i have 6 different categories
Angius
Angius10mo ago
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
ShuajbM
ShuajbMOP10mo ago
<div class="col-lg-3">
<!-- Shfaqeni dropdown-in me kategoritë nga ViewBag.Categories -->
<select name="kategoria" class="form-control">
<option value="">Zgjidh Kategorinë</option>
@foreach (var kategoria in ViewBag.Categories)
{
<option value="@kategoria">@kategoria</option>
}
</select>
</div>
<div class="col-lg-3">
<!-- Shfaqeni dropdown-in me kategoritë nga ViewBag.Categories -->
<select name="kategoria" class="form-control">
<option value="">Zgjidh Kategorinë</option>
@foreach (var kategoria in ViewBag.Categories)
{
<option value="@kategoria">@kategoria</option>
}
</select>
</div>
Angius
Angius10mo ago
So, yes, I was correct
ShuajbM
ShuajbMOP10mo ago
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 :
c#
public class Punet
{
[Key]
public int Id { get; set; }
[Required]
public string Name { get; set; }
[Required]
public string Description { get; set; }
[Required]
public string Kerkesat { get; set; }
[Required]
public string Lokacioni { get; set; }

public string kategoria { get; set; }

public string ImageUrl { get; set; }
}
}
c#
public class Punet
{
[Key]
public int Id { get; set; }
[Required]
public string Name { get; set; }
[Required]
public string Description { get; set; }
[Required]
public string Kerkesat { get; set; }
[Required]
public string Lokacioni { get; set; }

public string kategoria { get; set; }

public string ImageUrl { get; set; }
}
}
Angius
Angius10mo ago
And I just told you how to pass your list of items via the model, instead of using the viewbag
ShuajbM
ShuajbMOP10mo ago
first what to write at asp-items i try
Angius
Angius10mo ago
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"
ShuajbM
ShuajbMOP10mo ago
No description
ShuajbM
ShuajbMOP10mo ago
now its displaying the categories but i dont want to see the others Microsoft.aspnetcore...
<select asp-items="@ViewBag.Categories" name="kategoria" class="form-control">
<option value="">Zgjidh Kategorinë</option>
@foreach (var kategoria in ViewBag.Categories)
{
<option value="@kategoria">@kategoria</option>
}
</select>
<select asp-items="@ViewBag.Categories" name="kategoria" class="form-control">
<option value="">Zgjidh Kategorinë</option>
@foreach (var kategoria in ViewBag.Categories)
{
<option value="@kategoria">@kategoria</option>
}
</select>
Angius
Angius10mo ago
Don't display the options manually, then
<select asp-items="@ViewBag.Categories" name="kategoria" class="form-control">
- <option value="">Zgjidh Kategorinë</option>
- @foreach (var kategoria in ViewBag.Categories)
- {
- <option value="@kategoria">@kategoria</option>
- }
</select>
<select asp-items="@ViewBag.Categories" name="kategoria" class="form-control">
- <option value="">Zgjidh Kategorinë</option>
- @foreach (var kategoria in ViewBag.Categories)
- {
- <option value="@kategoria">@kategoria</option>
- }
</select>
ShuajbM
ShuajbMOP10mo ago
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
Angius
Angius10mo ago
Well, you will have to handle that somehow
ShuajbM
ShuajbMOP10mo ago
No description
ShuajbM
ShuajbMOP10mo ago
if i choose "Teknologji" that display only two
Angius
Angius10mo ago
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 that
ShuajbM
ShuajbMOP10mo ago
it seems hard
Angius
Angius10mo ago
No, it doesn't, really
<select asp-items="@Model.Categories" name="category"></select>
<select asp-items="@Model.Categories" name="category"></select>
public async Task<IActionResult> GetFilteredJobs([FromQuery] int category)
{
var jobs = _context.Jobs
.Where(j => j.Category.Id == categoryId)
.Select(j => new JobDto {
// ...
})
.ToListAsync();
return View(jobs);
}
public async Task<IActionResult> GetFilteredJobs([FromQuery] int category)
{
var jobs = _context.Jobs
.Where(j => j.Category.Id == categoryId)
.Select(j => new JobDto {
// ...
})
.ToListAsync();
return View(jobs);
}
ShuajbM
ShuajbMOP10mo ago
so i need to create a new method to my controller
Angius
Angius10mo ago
You can use the same method, sure Just make the parameter nullable
ShuajbM
ShuajbMOP10mo ago
so when i try
c#
var jobs=_unitOfWork.Punet.Where
c#
var jobs=_unitOfWork.Punet.Where
the Punet doesnt have this method
Angius
Angius10mo ago
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 there
ShuajbM
ShuajbMOP10mo ago
okay int GetPunetByCategory(); is this ok then i implement this to PunetRepository int GetPunetByCategory(int category);
Angius
Angius10mo ago
You only want to get an integer out of it?
ShuajbM
ShuajbMOP10mo ago
no
Angius
Angius10mo ago
I thought you wanted a list of punets in a given category Adjust accordingly, then
ShuajbM
ShuajbMOP10mo ago
so IEnumerable<string> GetPunetByCategory?
Angius
Angius10mo ago
You only want to get the strings? Like, only punet names? Or whole, entire punets?
ShuajbM
ShuajbMOP10mo ago
actually i want all the props for that job as displayed
Angius
Angius10mo ago
Adjust accordingly, then
ShuajbM
ShuajbMOP10mo ago
so this may be correct : IEnumerable<Punet> GetPunetByCategory();
Gramore
Gramore10mo ago
Almost: how would that method know what category you are looking for?
Angius
Angius10mo ago
Yes, that would be the correct return type
Gramore
Gramore10mo ago
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.
ShuajbM
ShuajbMOP10mo ago
okay let me try as a list now how to implement public List<Punet> GetPunetByCategory() { return _db.Punet.Where(j=>j.kategoria) } shouldi pass
Angius
Angius10mo ago
Well, you need to give this method something to filter by
ShuajbM
ShuajbMOP10mo ago
a param to the method
Angius
Angius10mo ago
It would be useful, yes Otherwise, what will you filter by?
ShuajbM
ShuajbMOP10mo ago
nothing haha so string kategoria ? or Punet p public List<Punet> GetPunetByCategory(Punet p) { return _db.Punet.Where(j =>j.kategoria == p.kategoria); }
Angius
Angius10mo ago
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
ShuajbM
ShuajbMOP10mo ago
public List<Punet> GetPunetByCategory(int categoryId) { return _db.Punet.Where(j =>j.kategoria == categoryId); }
Angius
Angius10mo ago
Is j.kategoria an int?
ShuajbM
ShuajbMOP10mo ago
no as a string so maybe its good that string kategori
Angius
Angius10mo ago
You cannot compare it to an int, then
ShuajbM
ShuajbMOP10mo ago
to the params
Angius
Angius10mo ago
That would work, yes
ShuajbM
ShuajbMOP10mo ago
No description
Angius
Angius10mo ago
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
ShuajbM
ShuajbMOP10mo ago
yes in fact that would be good but i had some problems with foreign key
Angius
Angius10mo ago
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 variant
ShuajbM
ShuajbMOP10mo ago
its not working idk thank you anyway
Want results from more Discord servers?
Add your server