1

I'm running into a problem where my DropDownListFor is not defaulting to the selected value that I've created in my new List. What am I doing wrong? I've looked up many of the solutions that was provide, but none seem to fit what I was doing.

Here is my Controller Code:

List<SelectListItem> queryPlanGroupList = new List<SelectListItem>();
queryPlanGroupList = (from t in db.PlanGroups
                      orderby t.Name ascending
                      select new SelectListItem()
                      {
                          Text = t.Name,
                          Value = t.ID.ToString(),
                          Selected = (t.ID == plans.PlanGroup.ID)
                      }).ToList();
ViewBag.PlanGroupList = queryPlanGroupList;

Here is my View Code:

<div class="form-group">
     @Html.LabelFor(model => model.PlanGroup, htmlAttributes: new { @class = "control-label" })
     @Html.DropDownListFor(model => model.PlanGroup, (List<SelectListItem>)ViewBag.PlanGroupList , "- Select one -", new { @class = "form-control" })
     @Html.ValidationMessageFor(model => model.PlanGroup, "", new { @class = "text-danger" })
</div>
user3813162
  • 47
  • 2
  • 10

2 Answers2

1

The Html.DropDownListFor method uses the first parameter (the expression which specifies your view model proeprty) value to set the selected option. The helper method also discard any Selected attribute you set on the SelectListItems to the default value (false).

So you should set the PlanGroup property value in your GET action method to the value you want to be pre-selected when the SELECT element is rendered.

var queryPlanGroupList = (from t in db.PlanGroups
                      orderby t.Name ascending
                      select new SelectListItem()
                      {
                          Text = t.Name,
                          Value = t.ID.ToString()
                      }).ToList();
ViewBag.PlanGroupList = queryPlanGroupList;

yourViewModel.PlanGroup = plans.PlanGroup.ID;
return View(yourViewModel);

Assuming PlanGroup property of your view model is of same type as the ID property of PlanGroup class.

Another option is to use the Html.DropDownList helper, which respects the Selected attribute on the SelectListItem

The below should also work with your current action method code.

@Html.DropDownList("PlanGroupList", null, "Select one", new { @class = "form-control" })

I personally prefer to use Html.DropDownListFor over Html.DropDownList as it is more strongly typed IMHO. I also prefer view model properties instead of using ViewBag to pass the list of options :)

Shyju
  • 214,206
  • 104
  • 411
  • 497
  • Curious, would the OP's code work if they hadn't chosen to use `Html.DropDownListFor` with strongly-typed viewmodels and instead used `Html.DropDownList`? With implementation like so: `@Html.DropDownList("PlanGroupList", null, "- Select one -", new{@class = "form-control" })`? – Grizzly Jun 25 '18 at 19:20
  • 1
    It would. Like i mentioned the `Html.DropDownListFor` is the one uses the property to set the selected option. I prefer to use `Html.DropDownListFor` over `Html.DropDownList` as it is more strongly typed IMHO. – Shyju Jun 25 '18 at 19:22
  • I agree that `Html.DropDownListFor` should be used more often, also because it's strongly typed it allows for jquery validation – Grizzly Jun 25 '18 at 19:22
0

I believe it is because you need to set the default value using your model. In your ViewModel, set the PlanGroup to be the value you want to be selected by default.

The Selected field for a SelectList is only used for DropDownList, not DropDownListFor.

Edit: Turns out that is the case. See a similar question here.

Michael Ziluck
  • 599
  • 6
  • 19