根据另一个linq查询的值更新所选的下拉列表值



我使用以下代码来获取dropdownlist的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可以访问要更新的值。我可以知道如何实现我的目标吗?

使用此

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

您可以通过以下方式实现:

ViewData["dropDown_Color"] = new SelectList(YourSelectList, "Value", "Text", selectedValue);

最新更新