C
C#8mo 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
ShuajbM8mo 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
Angius8mo ago
How are you displaying them now? Or just ask a question and dip, sure, that's also fine
ShuajbM
ShuajbM8mo 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
Angius8mo 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
ShuajbM8mo ago
so instead of ToList method to switch to SelectListItem oh okay from SelectList what to use instead of
ViewBag
ViewBag
Angius
Angius8mo 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
ShuajbM8mo ago
where to get that Id
Angius
Angius8mo ago
It's an example
ShuajbM
ShuajbM8mo ago
cuz im not going to get that data from a foreign key ah okay
Angius
Angius8mo 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
ShuajbM8mo ago
No description
ShuajbM
ShuajbM8mo ago
its showing those 6 records the category attribute in my database is stored as a string i have 6 different categories
Angius
Angius8mo 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
ShuajbM8mo 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>
Want results from more Discord servers?
Add your server