4

I am trying to apply bootstrap style(same as the one given here) to a dropdown. I am using razor syntax to display the dropdown. The items of the dropdown are coming from another .cs file.
I have followed some of the posts here in SO, but still not getting the right way to do it.
It is just getting displayed as a plan dropdown without any bootstrap style applied.

<div class="col-md-2 col-lg-2">
        <button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown" aria-expanded="true">
            @Html.DropDownListFor(m => m.Menu, new SelectList(Dropdown.DDMenu(), "Value", "Text"), new { @class = "form-control" })
        </button>

</div>

The dropdown menu is not displayed when I add the <button></button> but when I remove it, it works.

2
  • Your code will create a select tag inside the button and that is not valid. You should follow the documentation and use <ul> and <li> instead Commented May 19, 2016 at 12:07
  • @oussama_gd but the dropdown items are defined in a separate class file, how can I follow <ul> and <li>? din't get you. In the entire application we have defined all the dropdowns in a separate file and I cannot declare the dropdown items inside the view Commented May 19, 2016 at 12:20

1 Answer 1

6

I mean something like this

<div class="dropdown">
    <button class="btn btn-primary dropdown-toggle" type="button" data-toggle="dropdown">
        Dropdown Example
        <span class="caret"></span>
    </button>
    <ul class="dropdown-menu">
        @foreach (var item in Dropdown.DDMenu())
        {
            <li><a href="#" data-value="@item.Value">@item.Text</a></li>
        }
    </ul>
</div>
Sign up to request clarification or add additional context in comments.

Comments

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.