0

次のコードを使用して、ドロップダウン リストの LOV を取得し、選択した値も設定します。

ViewData["dropDown_Color"] = correspondingDropDownValue
                                .Select(j => 
                                    new SelectListItem { 
                                            Text = j.ListOfValue, 
                                            Value = j.ListOfValue, 
                                            Selected = j.ListOfValue 
                                                            == x.DefaultValue 
                                            })
                                .ToList();

にドロップダウンリストができたので、次のクエリでこのベースViewDataの選択された値を更新したいと思いますViewData["dropDown_Color"]

var userPref = from m in db.UserColorPref
               where m.UserID.Equals(userSessionID)
               select m;

更新する値には、 からアクセスできますuserPref.color。目的を達成する方法を教えてください。

4

2 に答える 2

2

これを使って

 List<SelectListItem> selectlist = ViewData["dropDown_Color"] as List<SelectListItem>;
            selectlist.ForEach(x =>
            {
                x.Selected = x.Value == userPref.color;

            });
于 2013-08-28T11:04:57.520 に答える
1

次のようにして達成できます。

ViewData["dropDown_Color"] = new SelectList(YourSelectList, "Value", "Text", selectedValue);
于 2013-08-28T12:17:27.353 に答える