尝试从 Linq 查询 MVC "List doesn't contain definiton for ..." 错误填充 ViewModel 列表”

Trying to Populate ViewModel List from Linq query MVC "List doesn't contain definiton for ..."error"

我正在(尝试)构建一个多项选择测试应用程序,它将多项选择问题列表(select 列表)从控制器传递到视图。 然后使用 foreach 循环填充视图,post 将答案返回给控制器,检查它们并增加每个正确答案的分数,然后更新数据库。

我正在尝试使用 Linq 查询填充视图模型列表(为了保持我的控制器精简,我通过服务层中的方法执行此操作)。

型号

Questions (db first)

namespace AccessEsol.Models
{
using System;
using System.Collections.Generic;

public partial class Question
{
    public Question()
    {
        this.ExamDets = new HashSet<ExamDet>();
    }

    public int QuID { get; set; }
    public string Text1 { get; set; }
    public string Text2 { get; set; }
    public string CorrectAnswer { get; set; }
    public string Foil1 { get; set; }
    public string Foil2 { get; set; }
    public string Foil3 { get; set; }
    public string Level_ { get; set; }
    public string GrammarPoint { get; set; }

    public virtual ICollection<ExamDet> ExamDets { get; set; }
}
}

考试

namespace AccessEsol.Models
{
using System;
using System.Collections.Generic;

public partial class Exam
{
    public Exam()
    {
        this.Candidates = new HashSet<Candidate>();
        this.ExamDets = new HashSet<ExamDet>();
    }

    public int ExamID { get; set; }
    public string Name { get; set; }
    public Nullable<System.DateTime> Date { get; set; }
    public int AdminID { get; set; }
    public string StartLevel { get; set; }

    public virtual Administrator Administrator { get; set; }
    public virtual ICollection<Candidate> Candidates { get; set; }
    public virtual ICollection<ExamDet> ExamDets { get; set; }
}
}

还有一个包含 CandID 的 Candidate 模型:

以及使用这些模型的视图模型:

namespace AccessEsol.Models
{
public class ExamQuestionsViewModel
{
    public int QuID { get; set; }
    public int CandID { get; set; }
    public int ExamId { get; set;  }
    public string Text1 { get; set; }
    public string Text2 { get; set; }
    public string CorrectAnswer { get; set; }
    public string Foil1 { get; set; }
    public string Foil2 { get; set; }
    public string Foil3 { get; set; }
    public string Level { get; set; }
    public string GrammarPoint { get; set; }

    public virtual ICollection<ExamDet> ExamDets { get; set; }

}
}

这是填充视图模型的方法:

        public static List<ExamQuestionsViewModel> AddQuestions()
    {

        AccessEsolDataEntities db = new AccessEsolDataEntities();

        string questionLevel = GetLevel(); 
        int currentCand = GetCandID();
        int currentExam = GetExamID(); 

        //model = new DataAccess().Populate();
        var qu = (from a in  db.Questions
                 where a.Level_ == questionLevel
                 select a).ToList(); 

    List<ExamQuestionsViewModel> exam = new List<ExamQuestionsViewModel>();


        foreach (var IDs in exam)
        {

            currentCand = exam.CandID;
            currentExam = exam.ExamId;

        }


      return (exam);
    }

我收到的错误消息是

'System.Collections.Generic.List<AccessEsol.Models.ExamQuestionsViewModel>'
does not contain a definition for 'ExamId' and no extension method 
'ExamId' accepting a first argument of type 
'System.Collections.Generic.List<AccessEsol.Models.ExamQuestionsViewModel>'      could be found (are you missing a using directive or an assembly
 reference?

我在这里做错了什么?非常感谢所有反馈。

请试试这个而不是你的 foreach:

foreach (var IDs in exam)
{    
    currentCand = IDs.CandID;
    currentExam = IDs.ExamId;    
}