2

In the controller Index I have the following:

ViewBag.Assignees = (await GetAllUsers()).Select(a =>
            new SelectListItem
            {
                Text = a.DisplayName,
                Value = a.Username,
                Selected = a.DisplayName == "John Smith"
            }).OrderBy(x => x.Text).ToList();

In the View, I have the following:

 @Html.DropDownListFor(model => model.Assignee, 
                       ViewBag.Assignees as List<SelectListItem>, 
                       "Select Assignee", 
                       new { id = "ddlAssignee", @class = "form-control"})

The dropdownlist populates as expected, however, the default (selected = true) value, which does exist, does not get set. Can someone advise what is wrong in the above?

UPDATE:

By Changing the SelectListItem.Value to a.DisplayName (same as SelectedListItem.Text) I achieved it. Still not sure what prevents the dropdownlist from displaying the item with Selected = true

amindomeniko
  • 417
  • 6
  • 23
  • Look at my answer [here](https://stackoverflow.com/questions/57312206/edit-view-droplist-select-option-dislaying-the-current-value/57332068#57332068) and see if it helps – Bosco Aug 07 '19 at 17:16
  • @Bosco your solution populates the dropdownlist and displays the first item on the list. In my scenario, the default value is dynamic. – amindomeniko Aug 07 '19 at 17:55

4 Answers4

0

If the model.Assignee comes with value, and if it is an int it will be defaulted to 0, it will override the SelectListItem selected value.

I suggest to set up the model.Assignee.

KodiakMx
  • 236
  • 2
  • 11
  • Adding a new property to my model just to assign a default value does not seem the best approach. I have multiple of these dropdownlists in my page and would to stay away from this as much as I can. Any other workaround? – amindomeniko Aug 07 '19 at 17:56
0
Here two ways that i use.
WAY 1

@Html.DropDownListFor(model => model.Assignee, 
                      ViewBag.Assignees as List<SelectListItem>, 
                      "Value", // property to be set as Value of dropdown item
                      "Text",  // property to be used as text of dropdown item
                      "1"), // value that should be set selected of dropdown
                      new { id = "ddlAssignee", @class = "form-control"})


WAY 2 

<select name="SelectName" value="1" class="w-100">
  @foreach (var item in ViewBag.Collection) {
    <option value="@item.Id">@item.Name</option>
  }
</select>

I hope it work for you

@Html.DropDownListFor how to set default value

Community
  • 1
  • 1
Hector Lara
  • 182
  • 1
  • 10
0

In your view set:

 @Html.DropDownListFor(model => model.Assignee, 
                       ViewBag.Assignees as List<SelectListItem>, 
                       "Select Assignee", 
                       new { id = "ddlAssignee", @class = "form-control", @value = "Default value"})
Ransomware0
  • 427
  • 3
  • 10
0

When you have list already defined. Use This

@Html.DropDownList("CoverageDropDown", new SelectList(Model.youList, "Code", "Description",item.seletecItem), "Select")