New to C# and MVC, so probably an obvious error: My Controller logic does not send any db items to the View. I don't know what I'm doing wrong.
I am building a multiple choice online test and have two db entitiy classes
public partial class Answer
{
public int ID { get; set; }
public string CorrectAnswer { get; set; }
public string Foil1 { get; set; }
public string Foil2 { get; set; }
public string Foil3 { get; set; }
}
public partial class Question
{
public int ID { get; set; }
public string Text { get; set; }
public string Level { get; set; }
public string GrammarPoint { get; set; }
}
I want to display questions and answer options in a View so I have this ViewModel
public class ExamViewModel
{
public string CorrectAnswer { get; set; }
public string Foil1 { get; set; }
public string Foil2 { get; set; }
public string Foil3 { get; set; }
public string Text { get; set; }
}
This is the Controller.
public ActionResult TakeTest()
{
ActiveTestEntities db = new ActiveTestEntities();
ExamViewModel exam = new ExamViewModel();
exam.Text = db.Questions.FirstOrDefault(t => t.ID == 1).ToString();
exam.Foil1 = db.Answers.FirstOrDefault(t => t.ID == 1).ToString();
exam.Foil2 = db.Answers.FirstOrDefault(t => t.ID == 1).ToString();
exam.Foil3 = db.Answers.FirstOrDefault(t => t.ID == 1).ToString();
exam.CorrectAnswer = db.Answers.FirstOrDefault(t => t.ID == 1).ToString();
return View(exam);
}
The LINQ is supposed to send db items to this View:
@model AccessEsol.Models.ExamViewModel
@{
ViewBag.Title = "TakeTest";
}
<h2>TakeTest</h2>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<div class="display-label">
<h3>Click the correct answer:</h3>
</div>
<div class="display-field">
@Html.DisplayFor(model => model.Text )
</div>
<input id="Checkbox1" type="checkbox" />
@Html.DisplayFor(model => model.Foil1)
<input id="Checkbox1" type="checkbox" />
@Html.DisplayFor(model => model.Foil2)
<input id="Checkbox1" type="checkbox" />
@Html.DisplayFor(model => model.CorrectAnswer )
<input id="Checkbox1" type="checkbox" />
@Html.DisplayFor(model => model.Foil3)
<p>
<input type="submit" value="Submit Answers" />
</p>
</fieldset>
}
But beside each Checkbox displays AccessEsol.Models.Answer only, the db items have not been retrieved.
Much appreciated if you can tell me what I am doing wrong.