title: Linq within ASP.NET MVC controller syntax
body: <p>I am very new to ASP.NET MVC so please be kind.</p>
  
  <p>I have a very simple db with three tables Questions, QuestionOptions, Responses.</p>
  
  <p>my pk in Questions is Id and links to questionId in both QuestionOptions and Responses. I also have a field in Questions called pageId, so multiple questions can appear on one page.</p>
  
  <p>I started with this in my controller;</p>
  
  <pre><code>    // GET: /Questions/ViewQuestion/5
      public ActionResult ViewQuestion(int? id)
      {
          if (id == null)
          {
              return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
          }
          Question question = db.Questions.Find(id);
          if (question == null)
          {
              return HttpNotFound();
          }
          return View(question);
      }
  </code></pre>
  
  <p>And my View returns a single question and options perfectly;</p>
  
  <pre><code>@model Template.Models.Question
  
  @{
      ViewBag.Title = "Details";
  }
  &lt;div&gt;
      &lt;h4&gt;Question #@Model.questionId&lt;/h4&gt;
      &lt;hr /&gt;
  &lt;h1&gt; @Html.DisplayFor(model =&gt; model.question1)&lt;/h1&gt;
  &lt;ul&gt;
      @foreach (var item in Model.QuestionOptions)
      {
          &lt;li&gt;@Html.DisplayFor(modelItem =&gt; item.questionOption1)&lt;/li&gt;
      }
  &lt;/ul&gt;
  </code></pre>
  
  <p></p>
  
  <p>But now I am trying to create a view that will display all of the questions with the same pageID - and am getting nowhere.</p>
  
  <p>This is my new ActionResult;</p>
  
  <pre><code>        public ActionResult ViewQuestion(int? id)
      {
          var pageId = id;
          var question = from q in db.Questions
                         where q.pageId == pageId
                         orderby q.ranking
                         select new { q.questionId,
                         q.questionType, q.question1}
                         ;
  
          return View(question.ToList());
      }
  </code></pre>
  
  <p>And my View;</p>
  
  <pre><code>@model IEnumerable&lt;Template.Models.Question&gt;
  
  @{
      ViewBag.Title = "View question;";
  }
  
  &lt;div&gt;
      @foreach (var item in Model) {
      &lt;h4&gt;Question &lt;/h4&gt;
      &lt;hr /&gt;
      &lt;h1&gt; @Html.DisplayFor(model =&gt; item.question1.AsEnumerable())&lt;/h1&gt;
  
      &lt;ul&gt;
          @foreach (var item in Model.QuestionOptions)
          {
              &lt;li&gt;@Html.DisplayFor(modelItem =&gt; item.questionOption1)&lt;/li&gt;
      }
  &lt;/ul&gt;
  }
  </code></pre>
  
  <p></p>
  
  <p>But now it does not recognize <code>QuestionOptions</code> as part of the Model anymore, and even if I comment that section out I still get the error The model item passed into the dictionary is of type -- but this dictionary requires a model item of type <code>System.Collections.Generic.IEnumerable</code></p>
  
  <p>I know somewhere my syntax is terribly wrong, but I have no idea where after searching for 3 days. Please help!</p>