使用system.linq.lookup值为dropdownfor mvc 4

时间:2014-05-28 08:49:04

标签: asp.net-mvc-4 collections lambda html.dropdownlistfor

在MVC 4中,我使用razor从集合中获取项目并将其分配给var。 var的类型为

{System.Linq.Lookup<<>f__AnonymousType0<string,System.Guid,string>,IMEModels.InterviewManagement.Interviewer>.Grouping}

这是我的代码

 var ChairList = Model.Interviewers.Where(d => d.LocKey == Convert.ToString(location.LocationKey) && d.IsChair && d.Date == date.Date).GroupBy(x => new { x.FullDetails, x.InterviewerId, x.Preference }).ToList();

它看起来如何:

-       ChairList   Count = 1   System.Collections.Generic.List<System.Linq.IGrouping<<>f__AnonymousType0<string,System.Guid,string>,IMEModels.InterviewManagement.Interviewer>>
-       [0] {System.Linq.Lookup<<>f__AnonymousType0<string,System.Guid,string>,IMEModels.InterviewManagement.Interviewer>.Grouping} System.Linq.IGrouping<<>f__AnonymousType0<string,System.Guid,string>,IMEModels.InterviewManagement.Interviewer> {System.Linq.Lookup<<>f__AnonymousType0<string,System.Guid,string>,IMEModels.InterviewManagement.Interviewer>.Grouping}
+       [System.Linq.Lookup<<>f__AnonymousType0<string,System.Guid,string>,IMEModels.InterviewManagement.Interviewer>.Grouping] {System.Linq.Lookup<<>f__AnonymousType0<string,System.Guid,string>,IMEModels.InterviewManagement.Interviewer>.Grouping} System.Linq.Lookup<<>f__AnonymousType0<string,System.Guid,string>,IMEModels.InterviewManagement.Interviewer>.Grouping
-       Key { FullDetails = "TEST - Richard Jackson - (80020937)", InterviewerId = {ff1efad7-7176-4fab-a1bb-30f6656c8880}, Preference = "Available" }   <Anonymous Type>
        FullDetails "TEST - Richard Jackson - (80020937)"   string
+       InterviewerId   {ff1efad7-7176-4fab-a1bb-30f6656c8880}  System.Guid
        Preference  "Available" string
+       Raw View        

我想将它用于下拉列表,但它并没有认识到我为它提供的关键和价值:

@Html.DropDownListFor(m => m.InterviewSchedules[location.InterviewDates.IndexOf(date)].ChairId, new SelectList(ChairList, "InterviewerId", "FullDetails"))

有人可以帮我处理这段代码吗?可能有一种更容易的方法,我不知道这样做。

1 个答案:

答案 0 :(得分:1)

查看

                @Html.DropDownListFor(m => m.InterviewSchedules[location.InterviewDates.IndexOf(date)].ChairId,, ViewData["ReturnList"] as SelectList, new { @class = "form-control" })

代码(以viewData格式返回)

 public SelectList ReturnList(Guid UID) {
            var ChairList = Model.Interviewers.Where(d => d.LocKey == Convert.ToString(location.LocationKey) && d.IsChair && d.Date == date.Date).GroupBy(x => new { x.FullDetails, x.InterviewerId, x.Preference }).ToList();
            List<SelectListItem> selectItems = ChairList.Select(s => new SelectListItem() {
                Text =FullDetails,
                Value = InterviewerId.ToString(),
                Selected =  false
            }
          ).ToList();
            selectItems.Insert(0, new SelectListItem() {
                Text = " --Select -- ",
                Value = null,
                Selected = false
            });
            SelectList selectList = new SelectList(selectItems, "Value", "Text");
            return selectList;
        }