选择列表C#中的下拉列表(选定值)

时间:2009-09-25 14:36:14

标签: asp.net-mvc .net-3.5 c#-3.0

好的,我有以下代码

   #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

然后在主ActionResult中进行以下操作......

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

这在视图上

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

现在如果设置了持续时间(即int Duration = 7;)那么我会期望选择7,但由于某种原因它不是。在我放弃尝试并做更有成效的事情之前的任何暗示?

TA

1 个答案:

答案 0 :(得分:3)

修好它。

更改:

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

要:

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

解决问题