Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

Bootstrap Styling for DropDownList

$
0
0

I'm still learning .NET (and .NET Core), and am having trouble trying to style a DropDownList. So I was hoping someone can help point me in the right direction.

Here's the (rough) code I've got in my Controller:

List<SelectListItem> myList = new List<SelectListItem>();
var myData = (from p in _context.myTable
    where p.Identity == "XYZ"
    select p).ToList();
foreach (var item in myData)
{
    foreach (var property in item.GetType().GetProperties())
    {
        string itemTitle = property.ToString();
        var value = property.GetValue(item);
        string itemValue = Convert.ToString(value);
        if (itemValue != "")
        {
            myList.Add(new SelectListItem
            {
                Text = itemValue,
                Value = itemValue
            });
        }
    }

}
ViewData["MyListOfThings"] = myList;

And here's what I've got in my Razor View:

@Html.DropDownList("MyListOfThings")

It works, and my dropdown populates, but I'd like to add some CSS styling (Bootstrap in this case). I tried a variety of things, but I can't seem to figure out what I need to do so that I can add the "form-control" class to the DropDownList object. Can someone give me a nudge?

Thanks!


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>