How to use Dropdown list in cshtml
Asked Answered
L

2

6

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

Leathers answered 6/3, 2015 at 15:43 Comment(1)
Could you please clarify your intent? Your question is open ended and vague.Project
G
8

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>
Guyon answered 10/3, 2015 at 20:1 Comment(0)
T
2

Try replacing your last line with this:

@Html.DropDownListFor(model => model.PortORterminal, listItems, "AtlantaPort", new { @class = "form-control", } )
Thermostatics answered 21/12, 2015 at 19:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.