Click here to Skip to main content
15,887,825 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to get a DropDownListFor to display with the bootstrap style, but everything I've tried has failed to render the control as desired. How do I make it happen?

What I have tried:

I've NuGet'd bootstrap-select, and did this in BundlesConfig.cs

C#
bundles.Add(new ScriptBundle("~/bundles/bootstrap").
        Include("~/Scripts/bootstrap.js",
                "~/Scripts/bootstrap-select.js",
                "~/Scripts/respond.js"));

bundles.Add(new StyleBundle("~/Content/css").
        Include("~/Content/bootstrap.css",
                "~/Content/bootstrap-select.css",
                "~/Content/site.css"));


And my razor code looks like this:

C#
@Html.DropDownListFor(model => model.MyList, 
                      new SelectList(...), " - Select or Add -", 
                      new { htmlAttributes = new { @class = "form-control" } })
Posted
Updated 17-Oct-17 10:03am

1 solution

The htmlAttributes part is only required if you're using EditorFor. With DropDownListFor, or TextBoxFor, or any of the other helpers which render a single control, the object contains the attributes directly:
@Html.DropDownListFor(model => model.MyList, 
                      new SelectList(...), " - Select or Add -", 
                      new { @class = "form-control" })

Html.EditorFor and htmlAttributes[^]
 
Share this answer
 
Comments
#realJSOP 18-Oct-17 7:31am    
Many thanks, oh Great and Powerful Oz.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900