0

I have a view model called SkillLevelViewModel which contains two properties, an Id and a value. Based on the organizationId I pass in I return a SelectList of these Id(s) and value(s). I am able to see a select list iterated in my view for the number of times the organization matches, however, I am not able to set the selected item in each of these drop downs dynamically.

Assume the data looks like this

SkillLevel table

IF I add an integer value as the last argument in my controller I will see that item selected in the view but what I am struggling with is getting the value for each row returned from the database; I tried using data.SkillLevelId but I receive this error: 'List' does not contain a definition for 'SkillLevelId'

-- THIS WORKS but is not dynamic

ViewBag.SkillLevelList = new SelectList(data, "SkillLevelId", "SkillLevel", 2); 

ViewModel

public class SkillLevelViewModel
{
    public int SkillLevelId { get; set; }
    public string SkillLevel { get; set; }
}

View

@Html.DropDownList("skillLevel", (SelectList)ViewBag.SkillLevelList)

Controller

var data = GetSkillLevels(userInfo.OrganizationId);

ViewBag.SkillLevelList = new SelectList(data, "SkillLevelId", "SkillLevel", SET SELECTED VALUE OF EACH DROP DOWN WITH SkillLevelId);

public List<SkillLevelViewModel> GetSkillLevels(int organizationId)
{
    var skillLevels = new List<SkillLevelViewModel>();

    using (Entity db = new Entity())
    {
        var data = from sl in db.SkillLevels
                   where sl.Organization_ID == organizationId
                   select new SkillLevelViewModel()
                   {
                       SkillLevelId = sl.SkillLevel_ID,
                       SkillLevel = sl.SkillLevel1
                   };

            foreach(var item in data)
            {
                skillLevels.Add(item);
            }
        }

        return skillLevels;
    }
}
Brian Evans
  • 235
  • 1
  • 4
  • 21
  • Is `skillLevel` a property in the model your binding to? (note that the 4th parameter of the `SelectList` constructor is ignored when you binding to a model - its the value of the property which determines the selected option). And what do you mean by _each dropdown_ - are you generating dropdownlists in a loop? –  Jan 10 '17 at 00:25
  • Yes, the reason I created a select list is an employee can have 0 to many skills each of which has a skill level so I am creating a skill level drop down list for each of those skills and I would like to see the associated id selected in the control. I am not binding to anything else just the code I have shown. – Brian Evans Jan 10 '17 at 00:28
  • Then you need to have a model with a property for each skill (or a collection of skills) and bind to that property - e.g. `@Html.DropDownListFor(m => m.Skill1Level, (SelectList)ViewBag.SkillLevelList)` and if the value of `Skill1Level` is `3` then the "Advanced" option will be selected. –  Jan 10 '17 at 00:32
  • Note also you could simplify your code by having `GetSkillLevels()` return `IEnumerable` rather than `List` and then creating an `IEnumerable` from it. –  Jan 10 '17 at 00:34
  • @StephenMuecke, I will look into this. I think I understand what you mean. – Brian Evans Jan 10 '17 at 00:38
  • Just as a side note, if your model contains a collection of skills and you want to generate the dropdownlists in a loop, refer [this answer](http://stackoverflow.com/questions/37407811/mvc5-razor-html-dropdownlistfor-set-selected-when-value-is-in-array/37411482#37411482) –  Jan 10 '17 at 00:40

0 Answers0