Model view controller 试图从Linq查询MVC中填充ViewModel列表“列表不包含…”的定义”错误
我正在尝试构建一个选择题测试应用程序,该应用程序将从控制器向视图传递一系列选择题选择列表。 然后使用foreach循环填充视图,将答案发布回控制器,检查答案并增加每个正确答案的分数,然后更新数据库 我试图使用Linq查询填充视图模型列表以保持控制器精简,我是通过服务层中的一种方法来实现这一点的 模型Model view controller 试图从Linq查询MVC中填充ViewModel列表“列表不包含…”的定义”错误,model-view-controller,viewmodel,Model View Controller,Viewmodel,我正在尝试构建一个选择题测试应用程序,该应用程序将从控制器向视图传递一系列选择题选择列表。 然后使用foreach循环填充视图,将答案发布回控制器,检查答案并增加每个正确答案的分数,然后更新数据库 我试图使用Linq查询填充视图模型列表以保持控制器精简,我是通过服务层中的一种方法来实现这一点的 模型 Questions (db first) namespace AccessEsol.Models { using System; using System.Collections.Generic;
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
{
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;
}
foreach (var IDs in exam)
{
currentCand = IDs.CandID;
currentExam = IDs.ExamId;
}