2
如何以最简洁的方式更改列表中单个项目上的单个属性?更改列表中项目的属性
public static class QuestionHelper
{
public static IEnumerable<SelectListItem> GetSecurityQuestions()
{
return new[]
{
new SelectListItem { Value = "What was your childhood nickname?", Text = "What was your childhood nickname?"},
new SelectListItem { Value = "What is the name of your favorite childhood friend?", Text = "What is the name of your favorite childhood friend?"},
...
};
}
}
我要生成此列表中设置基于字符串的属性选择一个项目:
string selectText = "What is the name of your favorite childhood friend?";
form.SecurityQuestions = QuestionHelper.GetSecurityQuestions().Select(x => { /*Set Selected = true for SelectListItem where item.Text == selectedText */ });
return PartialView(form);
注:这必须考虑到,如果(selectedText == NULL)然后设置的第一项as selected
感谢您的编辑,以前的代码看起来不正确,有2个方法调用 – parliament