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>