简体   繁体   English

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

[英]Update the selected dropdownlist value base on value from another linq query

I use the following code to get the LOV for dropdownlist and setting a selected value as well: 我使用以下代码来获取下拉列表的LOV并设置选定的值:

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

Now that I have a dropdownlist in my ViewData , I want to update the selected value of this ViewData["dropDown_Color"] base on the following query 现在,我的ViewData有一个下拉列表,我想根据以下查询更新此ViewData["dropDown_Color"]的选定值

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

The value to be updated can be access by userPref.color . 可以通过userPref.color访问要更新的值。 May I know how to achieve my objective? 我可以知道如何实现我的目标吗?

Use this 用这个

 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM