SelectList dropdowns in C # (selected value)

Ok I have the following code

   #region getDurationListDD
    private List<KeyValuePair<int, int>> getDurationListDD
    {
        get
        {
            List<KeyValuePair<int, int>> dDur = new List<KeyValuePair<int, int>>();
            dDur.Add(new KeyValuePair<int, int>(2, 2));
            dDur.Add(new KeyValuePair<int, int>(3, 3));
            dDur.Add(new KeyValuePair<int, int>(4, 4));
            dDur.Add(new KeyValuePair<int, int>(7, 7));
            dDur.Add(new KeyValuePair<int, int>(14, 14));
            dDur.Add(new KeyValuePair<int, int>(21, 21));

            return dDur;
        }
    }
    #endregion

      

Then in the main ActionResult ...

ViewData["changeDuration"] = new SelectList(getDurationListDD, "Key", "Value", Duration);

      

it looks

Html.DropDownList("changeduration", (SelectList)ViewData["changeDuration"])

      

Now, if Duration was set (i.e. int Duration = 7;), I would expect 7 to be selected, but for some reason it is not. Any hints before I give up trying and do something more productive?

That

+2


source to share


1 answer


Just fixed it.

Changed:

Html.DropDownList("changeduration", (SelectList)ViewData["changeDuration"])

      



To:

Html.DropDownList("cduration", (SelectList)ViewData["changeDuration"])

      

Solution to the problem

+3


source







All Articles