我想生成一个具有唯一值的下拉列表。 但是我只能生成从数据库生成的原始下拉列表。
在我的语言表中它包含3种语言: -ENG,GER,KOR
我已用于问题列表的语言: - KOR
因此结果绑定到dropdownlist应该: - ENG,GER
我的控制器代码:
List<SelectListItem> langResult = new List<SelectListItem>();
///// Ori Language
var result = from r in db.SURV_Language_Model
select r.Language;
///// Used Language
var result2 = from r in db.SURV_Question_Ext_Model
join s in db.SURV_Question_Model on r.Qext_Question_ID equals s.Question_ID
orderby s.Question_Position ascending
where r.Qext_Question_ID == Question_ID
select new { r, s };
/////Remaining Language
result 3 = result 2 - result 1 <==*** Pseudo code
foreach (var item in result3)
{
SelectListItem temp = new SelectListItem();
temp.Text = item;
temp.Value = item;
langResult.Add(temp);
}
ViewBag.LangList = langResult;
我的观看代码:
@Html.LabelFor(model => model.Language)
@Html.DropDownListFor(model => model.Language, ViewBag.LangList as IEnumerable<SelectListItem>)
@Html.ValidationMessageFor(model => model.Language, "*")
无论如何要执行此操作?
答案 0 :(得分:0)
通过以下代码修复,希望可以帮助你们所有人:
List<SelectListItem> langResult = new List<SelectListItem>();
var result = from r in db.SURV_Language_Model
select r.Language;
var result2 = from r in db.SURV_Question_Ext_Model
join s in db.SURV_Question_Model on r.Qext_Question_ID equals s.Question_ID
orderby s.Question_Position ascending
where r.Qext_Question_ID == Question_ID
select r.Qext_Language;
List<string> list = new List<string>(result);
List<string> list2 = new List<string>(result2);
for (int x = 0; x < list.Count(); x++ )
{
if ( x < list2.Count())
{
if (list[0].ToString() == list2[x].ToString())
{
list.Remove(list[0].ToString());
}
}
}
foreach (var item in list)
{
SelectListItem temp = new SelectListItem();
temp.Text = item;
temp.Value = item;
langResult.Add(temp);
}
ViewBag.LangList = langResult;