0

How do I select an option in razor? For every item it can be an other value.

@Html.DropDownListFor(model => model.items[i].Enum, ViewBag.Enum as SelectList, new { class="enum")})

ViewBag.Enum = new SelectList(new[]
{
    new SelectListItem {Text="Auto", Value="Auto" },
    new SelectListItem {Text="Man", Value="Man"},
    new SelectListItem {Text="Not", Value="Not" },
}, "Text", "Value");
  • Possible duplicate of [MVC5 Razor html.dropdownlistfor set selected when value is in array](http://stackoverflow.com/questions/37407811/mvc5-razor-html-dropdownlistfor-set-selected-when-value-is-in-array) –  May 26 '16 at 23:53
  • 1
    First, there is no point creating a 2nd `IEnumerable` from the first one using `new SelectList(....`. As per the dupe, use `ViewBag.Enum = new List(){ "Auto", "Man", "Not" };` and in the view `@Html.DropDownListFor(model => model.items[i].Enum, new SelectList(ViewBag.Enum, Model.items[i].Enum, new { class="enum" })` –  May 26 '16 at 23:56

0 Answers0