无法将 lambda 表达式转换为类型 'IEnumerable<SelectListItem>',因为它不是委托类型
Cannot convert lambda expression to type 'IEnumerable<SelectListItem>' because it is not a delegate type
视图模型
public class EmployeeIndexViewModel {
public int selectedDepartment { get; set; }
public IList departments1 { get; set; }
}
控制器操作
List<Department> list = new List<Department>();
list.Add(new Department { ID = 1, Name = "D1" });
list.Add(new Department { ID = 2, Name = "D2" });
list.Add(new Department { ID = 3, Name = "D3" });
employeeIndexViewModel.departments1 = list;
employeeIndexViewModel.selectedDepartment = "2";
查看
@Html.DropDownListFor(m => m.department, new SelectList(Model.departments1, "Id", "Name"))
现在,编码工作正常。但是我不喜欢这首曲子
new SelectList(Model.departments1, "Id", "Name")
我想将它添加到我的视图模型中,然后将它绑定到我的视图中。
所以我改变了我的 ViewModel
新视图模型
public class EmployeeIndexViewModel {
public int selectedDepartment { get; set; }
public IList departments1 { get; set; }
public IEnumerable<SelectListItem> departments2 { get; set; }//add this
}
新控制器动作
List<Department> list = new List<Department>();
list.Add(new Department { ID = 1, Name = "D1" });
list.Add(new Department { ID = 2, Name = "D2" });
list.Add(new Department { ID = 3, Name = "D3" });
employeeIndexViewModel.departments1 = list;
employeeIndexViewModel.selectedDepartment = "2";
employeeIndex2ViewModel.departments2 = new SelectList(list, "id", "name");// add this
新视图
@Html.DropDownListFor(m => m.department, new SelectList(Model.departments1, "Id", "Name"))
@Html.DropDownListFor(m => m.department, (SelectList)(m => m.departments2))//add this
我收到这个错误:
Cannot convert lambda expression to type 'IEnumerable<SelectListItem>' because it is not a delegate type
删除新视图中的 lambda 表达式
(SelectList)(m => m.departments2)
并替换为 Model.departments2
视图模型
public class EmployeeIndexViewModel {
public int selectedDepartment { get; set; }
public IList departments1 { get; set; }
}
控制器操作
List<Department> list = new List<Department>();
list.Add(new Department { ID = 1, Name = "D1" });
list.Add(new Department { ID = 2, Name = "D2" });
list.Add(new Department { ID = 3, Name = "D3" });
employeeIndexViewModel.departments1 = list;
employeeIndexViewModel.selectedDepartment = "2";
查看
@Html.DropDownListFor(m => m.department, new SelectList(Model.departments1, "Id", "Name"))
现在,编码工作正常。但是我不喜欢这首曲子
new SelectList(Model.departments1, "Id", "Name")
我想将它添加到我的视图模型中,然后将它绑定到我的视图中。 所以我改变了我的 ViewModel
新视图模型
public class EmployeeIndexViewModel {
public int selectedDepartment { get; set; }
public IList departments1 { get; set; }
public IEnumerable<SelectListItem> departments2 { get; set; }//add this
}
新控制器动作
List<Department> list = new List<Department>();
list.Add(new Department { ID = 1, Name = "D1" });
list.Add(new Department { ID = 2, Name = "D2" });
list.Add(new Department { ID = 3, Name = "D3" });
employeeIndexViewModel.departments1 = list;
employeeIndexViewModel.selectedDepartment = "2";
employeeIndex2ViewModel.departments2 = new SelectList(list, "id", "name");// add this
新视图
@Html.DropDownListFor(m => m.department, new SelectList(Model.departments1, "Id", "Name"))
@Html.DropDownListFor(m => m.department, (SelectList)(m => m.departments2))//add this
我收到这个错误:
Cannot convert lambda expression to type 'IEnumerable<SelectListItem>' because it is not a delegate type
删除新视图中的 lambda 表达式
(SelectList)(m => m.departments2)
并替换为 Model.departments2