Asp.net mvc asp.net mvc razor fill@Html.DropDownList来自两个表

Asp.net mvc asp.net mvc razor fill@Html.DropDownList来自两个表,asp.net-mvc,razor,html-select,Asp.net Mvc,Razor,Html Select,通过下面的代码,我得到了以下信息: <select id="ClarderSousSecteurID" name="ClarderSousSecteurID"> <option value="">--- Tous ---</option> <option value="23">23</option> <option value="46">46</option> <opti

通过下面的代码,我得到了以下信息:

<select id="ClarderSousSecteurID" name="ClarderSousSecteurID">
    <option value="">---  Tous  ---</option>
    <option value="23">23</option>
    <option value="46">46</option>
    <option value="29">29</option>
        ....
</select>
<select id="ClarderSousSecteurID" name="ClarderSousSecteurID">
    <option value="">---  Tous  ---</option>
    <option value="23">Libelle No 1</option>
    <option value="46">Libelle No 2</option>
    <option value="29">Libelle No 3</option>
        ....
</select>
控制器:

var data = from p in db.tblUSUAs
                       join f in db.tblClarderSousSecteur
                       on p.ClarderSousSecteurID equals f.ClarderSousSecteurID
                       select new
                       {
                           ClarderSousSecteurID = p.ClarderSousSecteurID,
                           ClarderSousSecteurLibelle = f.ClarderSousSecteurLibelle
                       };
            ViewBag.ClarderSousSecteurID = new SelectList(data.Select(a => a.ClarderSousSecteurID).Distinct().Select(c => new { ClarderSousSecteurID = c, ClarderSousSecteurLibelle = c }), "ClarderSousSecteurID", "ClarderSousSecteurLibelle", ClarderSousSecteurID);
多谢各位。
Nacer T.

查看您正在传递给
选择列表
构造函数的数据

 data.Select(a => a.ClarderSousSecteurID)
      .Distinct()
      .Select(c => new { ClarderSousSecteurID = c, ClarderSousSecteurLibelle = c })
您只选择了
数据
中项目的
clardersoursecteurid
。因此,表达式
data.Select(a=>a.clardersoursecteurid)
将为您提供一个整数集合(IQueryable of int),您将在以后的
Select
中为
clardersoursecteurid
clardersourlibelle
值使用该结果。这就是您获得选项文本和值的
clardersoursecteurid
值的原因

如果您的联接为您提供了唯一的结果,那么您根本不需要进行不同的调用。您可以将结果转换为SelectListItem列表

var data = (from p in db.tblUSUAs
           join f in db.tblClarderSousSecteur
           on p.ClarderSousSecteurID equals f.ClarderSousSecteurID
           select new SelectListItem
           {
              Value = p.ClarderSousSecteurID.ToString(),
              Text = f.ClarderSousSecteurLibelle
           }).ToList();
ViewBag.ClarderSousSecteurID = new SelectList(data, "Value","Text", ClarderSousSecteurID);
但是,如果LINQ联接给您的记录不是唯一的,并且您仍然希望基于CLARDersoursecteurid列值执行distinct,则可以在
CLARDersoursecteurid
属性上执行group by,并将结果转换为SelectListItem的集合

List<SelectListItem> uniqueListById = data.GroupBy(s => s.ClarderSousSecteurID,
                   p => p,
                  (k, v) => new SelectListItem { 
                                    Value = k.ToString(), 
                                    Text = v.FirstOrDefault().ClarderSousSecteurLibelle })
                                                                                 .ToList();

我把它改成这个。。。但是仍然没有使用wordking:new SelectList(data.Select(a=>a.clardersoursecteurid,a=>a.clardersoursecteurlibelle)LINQ to entities不重新识别方法System.String to String()我使用非分组方式解决方案更新了答案(如果您的记录在LINQ联接中是唯一的)。看一看。
ClarderSousSecteurID
的类型是什么?我的答案假设它是数字类型。如果它是字符串类型,只需删除
。ToString()
call.byte ClarderSousSecteurID…的类型是byte。因此….Value=k.ToString(),不被接受。我得到错误:当我将方法System.String ToString()更改为:Value=k,…。我得到:无法将类型“byte?”隐式转换为“String”。
List<SelectListItem> uniqueListById = data.GroupBy(s => s.ClarderSousSecteurID,
                   p => p,
                  (k, v) => new SelectListItem { 
                                    Value = k.ToString(), 
                                    Text = v.FirstOrDefault().ClarderSousSecteurLibelle })
                                                                                 .ToList();
ViewBag.ClarderSousSecteurID = new SelectList(uniqueListById, "Value","Text",
                                                                    ClarderSousSecteurID);