0

My drop down code is currently below and I know what I need is in this format:

IEnumerable<SelectListItem> selectlist

What I'm currently using is this code and I need to convert it to the ienumerable selectlistitem with the following information

<select id="paperStyle" name="paperStyleList">
        <option selected="selected" value="">Select Style</option>
        <option value="APA">APA</option>
        <option value="Chicago">Chicago</option>
        <option value="Harvard">Harvard</option>
        <option value="MLA">MLA</option>
        <option value="Oxford">Oxford</option>
</select>
  • 1
    This link might be userful http://stackoverflow.com/questions/867117/how-to-add-static-list-of-items-in-mvc-html-dropdownlist – faraaz Apr 10 '14 at 06:05

2 Answers2

1

something like this should work:

@Html.DropDownListFor(m => m.paperStyle, Model.paperStyleList.Select(item => new SelectListItem { Value = item, Text = item }), "Select Style")

or this:

@Html.DropDownListFor(m => m.paperStyle, new SelectList(Model.paperStyleList), "Select Style")

where paperStyle is a property of your model and paperStyleList is your list with values (APA,Chicago,etc) of type IEnumerable or List for that matter

dima
  • 1,181
  • 1
  • 9
  • 20
0

Try this,

Controller

[HttpGet]
        public ActionResult CustomerInfo()
        {

            List<SelectListItem> items = new List<SelectListItem>();
            items.Add(new SelectListItem() { Text = "Chicago", Value = "Chicago", Selected = false });
            items.Add(new SelectListItem() { Text = "APA", Value = "APA", Selected = false });
            items.Add(new SelectListItem() { Text = "Harvard", Value = "Harvard", Selected = false });
            items.Add(new SelectListItem() { Text = "Oxford", Value = "Oxford", Selected = false });

            ViewBag.paperStyle= new SelectList(items, "Value", "Text");
            return View();
        }

View

@Html.DropDownList("CustomerId", (SelectList)ViewBag.paperStyle, "--Select--")
Nilesh Gajare
  • 6,302
  • 3
  • 42
  • 73