C# 根据另一个linq查询的值更新选定的dropdownlist值
我使用以下代码获取dropdownlist的LOV,并设置一个选定值:C# 根据另一个linq查询的值更新选定的dropdownlist值,c#,asp.net-mvc,linq,C#,Asp.net Mvc,Linq,我使用以下代码获取dropdownlist的LOV,并设置一个选定值: ViewData["dropDown_Color"] = correspondingDropDownValue .Select(j => new SelectListItem { Text =
ViewData["dropDown_Color"] = correspondingDropDownValue
.Select(j =>
new SelectListItem {
Text = j.ListOfValue,
Value = j.ListOfValue,
Selected = j.ListOfValue
== x.DefaultValue
})
.ToList();
现在我的ViewData
中有了一个dropdownlist,我想根据下面的查询更新这个ViewData[“dropDown\u 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;
});
List-selectlist=ViewData[“下拉菜单颜色”]作为列表;
selectlist.ForEach(x=>
{
x、 所选=x.Value==userPref.color;
});
使用此
List<SelectListItem> selectlist = ViewData["dropDown_Color"] as List<SelectListItem>;
selectlist.ForEach(x =>
{
x.Selected = x.Value == userPref.color;
});
List-selectlist=ViewData[“下拉菜单颜色”]作为列表;
selectlist.ForEach(x=>
{
x、 所选=x.Value==userPref.color;
});
您可以通过以下方式实现:
ViewData["dropDown_Color"] = new SelectList(YourSelectList, "Value", "Text", selectedValue);
您可以通过以下方式实现:
ViewData["dropDown_Color"] = new SelectList(YourSelectList, "Value", "Text", selectedValue);
嗨@Jatinpatil,如果我错了,请纠正我,新的SelectList不是在列表中添加新项目吗?我没有试图将新项目添加到现有列表中,我只是想更新现有列表中的selectedValue否它不会在您的选择列表中添加任何新项目。您好@Jatinpatil,如果我错了,请更正,新的选择列表不是正在将新项目添加到列表中吗?我不想在现有列表中添加新项目,我只想更新现有列表中的selectedValue否它不会在您的选择列表中添加任何新项目。