1

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! What am I doing wrong?

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I also want to add that those missing @'s like this get me way more often than they should, heh.
@Mona Coder You should also consider adding values to your options <option value="@item">@item</option>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.