How to use Dropdown list in cshtml

user3543822 picture user3543822 · Mar 6, 2015 · Viewed 34.6k times · Source

How to use dropdown list in cshtml? I am using below code ,Now it looks like general HTML but problem is not apply the bootstrap styles.

<div class="col-md-10">

            @{
List<SelectListItem> listItems = new List<SelectListItem>();
listItems.Add(new SelectListItem
     {
         Text = "AtlantaPort",
         Value = "AtlantaPort"
     });
listItems.Add(new SelectListItem
     {
         Text = "NewJersyTerminal",
         Value = "NewJersyTerminal",
         Selected = true
     });
listItems.Add(new SelectListItem
     {
         Text = "AWS",
         Value = "AWS"
     });
            }

            @Html.DropDownListFor(model => model.PortORterminal, listItems, "AtlantaPort", new { htmlAttributes = new { @class = "form-control", } })                        )
        </div>

Thanks in Advance

Answer

Lucas Roselli picture Lucas Roselli · Mar 10, 2015

Use :

@Html.DropDownList("yourDropName", listItems, new { @class = "form-control" })

Your code edited:

         @{
List<SelectListItem> listItems = new List<SelectListItem>();
listItems.Add(new SelectListItem
  {
      Text = "AtlantaPort",
      Value = "AtlantaPort"
  });
listItems.Add(new SelectListItem
 {
     Text = "NewJersyTerminal",
     Value = "NewJersyTerminal",
     Selected = true
 });
listItems.Add(new SelectListItem
 {
     Text = "AWS",
     Value = "AWS"
 });
        }

 @Html.DropDownList("yourDropName", listItems, new { @class = "form-control" })
    </div>