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
ShuajbM10mo 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
ShuajbM10mo 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 :
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
ShuajbM10mo 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
ShuajbM10mo ago
where to get that Id
Angius
Angius10mo ago
It's an example
ShuajbM
ShuajbM10mo 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
ShuajbM10mo ago
No description
ShuajbM
ShuajbM10mo 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
ShuajbM10mo 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
ShuajbM10mo 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
ShuajbM10mo 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
ShuajbM10mo ago
No description
ShuajbM
ShuajbM10mo 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
ShuajbM10mo 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
ShuajbM10mo ago
No description
ShuajbM
ShuajbM10mo 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
ShuajbM10mo 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
ShuajbM10mo 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
ShuajbM10mo 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
ShuajbM10mo 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
ShuajbM10mo ago
no
Angius
Angius10mo ago
I thought you wanted a list of punets in a given category Adjust accordingly, then
ShuajbM
ShuajbM10mo ago
so IEnumerable<string> GetPunetByCategory?
Angius
Angius10mo ago
You only want to get the strings? Like, only punet names? Or whole, entire punets?
ShuajbM
ShuajbM10mo ago
actually i want all the props for that job as displayed
Angius
Angius10mo ago
Adjust accordingly, then
ShuajbM
ShuajbM10mo 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
ShuajbM10mo 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
ShuajbM10mo ago
a param to the method
Angius
Angius10mo ago
It would be useful, yes Otherwise, what will you filter by?
ShuajbM
ShuajbM10mo 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
ShuajbM10mo ago
public List<Punet> GetPunetByCategory(int categoryId) { return _db.Punet.Where(j =>j.kategoria == categoryId); }
Angius
Angius10mo ago
Is j.kategoria an int?
ShuajbM
ShuajbM10mo ago
no as a string so maybe its good that string kategori
Angius
Angius10mo ago
You cannot compare it to an int, then
ShuajbM
ShuajbM10mo ago
to the params
Angius
Angius10mo ago
That would work, yes
ShuajbM
ShuajbM10mo 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
ShuajbM10mo 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
ShuajbM10mo ago
its not working idk thank you anyway
Want results from more Discord servers?
Add your server