我有一个课程清单。如果将用户分配给课程,那么我希望选中该复选框。为什么不接受 Contains() ?
视图模型:
public class ViewUserViewModel
{
public List<Cours> Courses { get; set; }
public List<UserCours> UserCoursesList { get; set; }
public AspNetUser user { get; set; }
public IEnumerable<SelectListItem> CourseList { get; set; }
}
控制器:
[HttpGet]
public ActionResult ViewUser(string id)
{
ViewUserViewModel model = new ViewUserViewModel();
model.user = db.AspNetUsers.FirstOrDefault(U => U.Id == id);
//List all courses
List<Cours> allCourses = db.Courses.OrderBy(c => c.CourseName).ToList();
model.Courses = allCourses;
//List of courses the user is assigned to
//var selectedCourse1 = db.UserCourses.Where(uc => uc.UserId == id).ToList();
model.UserCoursesList = db.UserCourses.Where(uc => uc.UserId == id).ToList();
//checkbox list
model.CourseList = allCourses.ToList().Select(x => new SelectListItem()
{
//Selected = selectedCourse1.Contains(x.CourseID),
Selected = model.UserCoursesList.Contains(x.CourseID),
Text = x.CourseName,
Value = x.CourseID.ToString()
});
}
我在想 Selected 属性将测试 List 是否分配了值,然后它会返回 true。相反,它是一个语法错误并且有无效的参数。如何将 CourseList 与 UserCoursesList 进行比较?