Mona Coder Mona Coder - 1 month ago 6
C# Question

Issue on Populating Data on Drop-down List in ASP.Net MVC

I am trying to Load data from

Category
table into a Bootstrap Drop down list like:

<div class="form-group">
<label for="sel1"></label>
<select class="form-control" id="cat">
<option>Select From The List</option>
@{
DataEntities ctx = new DataEntities();
var cat = ctx.Categories.OrderByDescending(p => p.CategoryName);
foreach (var item in cat)
{
<option>item</option>
}
}
</select>
</div>


but I am getting this is result

enter image description here

the count of
item
is equal to number of rows in
Category
table but I am not getting the actual value! . Can you please let me know what i am doing wrong?

Answer

You're missing an @ symbol in your option tag.

foreach (var item in cat)
{
    <option>@item</option>
}

In your code, you're just adding the string "item" for each item in cat.