Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从selectlist查询填充下拉列表将返回dropdownlist中的System.Web.Mvc.SelectListItem_C#_Linq - Fatal编程技术网

C# 从selectlist查询填充下拉列表将返回dropdownlist中的System.Web.Mvc.SelectListItem

C# 从selectlist查询填充下拉列表将返回dropdownlist中的System.Web.Mvc.SelectListItem,c#,linq,C#,Linq,我有一个下拉列表,它工作得非常好,但必须进行编辑,因为我需要在第二个表上执行连接,我得出了以下结论 ViewBag.Type = new SelectList(from r in db.Departments join s in db.Payment on r.departmentId equals s.deparmentId_FK where r.myID == myID

我有一个下拉列表,它工作得非常好,但必须进行编辑,因为我需要在第二个表上执行连接,我得出了以下结论

ViewBag.Type = new SelectList(from r in db.Departments
                              join s in db.Payment on r.departmentId equals s.deparmentId_FK
                              where r.myID == myID
                              select new SelectListItem
                                         {
                                              Value = s.paymentTypeId.ToString(),
                                              Text = s.name
                                         }).ToList();
在我看来

@Html.DropDownList("Type", (IEnumerable<SelectListItem>)ViewBag.Type, "--- Tous ---")
@Html.DropDownList("Type", ViewBag.Type as IEnumerable<SelectListItem>, "Choose Type", new { @class = "form-control listbox" })
@Html.DropDownList("Type", new SelectList(ViewBag.Type, "Value", "Text"), "Choose Type", new { @class = "form-control listbox" })`
@Html.DropDownList(“Type”,(IEnumerable)ViewBag.Type,“--Tous--”)
@DropDownList(“Type”,ViewBag.Type为IEnumerable,“Choose Type”,new{@class=“form control listbox”})
@DropDownList(“Type”,new SelectList(ViewBag.Type,“Value”,“Text”),“Choose Type”,new{@class=“form control listbox”})`
所遇到的问题是,尽管项目被正确筛选,但我无法获得数据来显示除
System.Web.Mvc.SelectListItem
之外的任何内容。我已经采取了其他问题的步骤,但我无法动摇这个问题

如果我能知道遗漏了什么,我将不胜感激


请尝试选择一个新的匿名对象。根据您正在使用的
实体框架
的版本,问题可能是它试图评估服务器或客户端上的
Select
语句。它所做的任何一件事显然都不喜欢尝试选择新的
SelectListItem
。下面的例子

ViewBag.Type = new SelectList((from r in db.Departments
                              join s in db.Payment on r.departmentId equals s.deparmentId_FK
                              where r.myID == myID
                              select new
                                         {
                                              Value = s.paymentTypeId.ToString(),
                                              Text = s.name
                                         }).ToList());

遗憾的是,这不是问题所在,我添加了您的建议,但没有任何更改。此外,您的查看包已经是一个selectlist,因此请取出castI已更改的
select new selectlist项目
select new
,现在我得到
{value=1,text=apples}
我更新了我的答案,以反映您的发现@Niana。不幸的是,结果是一样的