Note that there are some explanatory texts on larger screens.

plurals
  1. POCan 2 Linq queries be passed to 1 View?
    text
    copied!<p>I have a view that is outputting a list of questions(yesno and multiple choice). For the Multiple Choice questions, I want to also pass the possible multiple choice answers but I can not figure out how to pass 2 queries. Is there a way to do this or is it better handled another way?</p> <p>Controller Code:</p> <pre><code>public ActionResult Test(int id) { var listofquestions = from m in db.vQuestionnaireQuestions where m.Questionnaire_ID.Equals(id) select m; return View("Test", listofquestions.ToList()); } </code></pre> <p>View Code:</p> <pre><code>&lt;% foreach (var item in Model) { %&gt; &lt;br /&gt; &lt;%: item.Question_Text %&gt; &lt;%if (item.Question_Type_ID == 1) //Yes-No Question { %&gt; //Yes-No Stuffs &lt;% } else if (item.Question_Type_ID == 2) //Multiple Choice { %&gt; //Can I access a Linq query again here? //I have Question_ID to use, but I don't think //I can have 2 Models &lt;% } else //All Else { %&gt; //All Else Stuffs &lt;% } } %&gt; </code></pre> <hr> <p>EDIT</p> <p>I've created a view model class</p> <p>View Model Class Code: </p> <pre><code>public IEnumerable&lt;vQuestionnaireQuestion&gt; FindAllQuestionnaireQuestionsTest() { return db.vQuestionnaireQuestions; } public vQuestionnaireQuestion GetQuestionnaireQuestionsTest(int id) { return db.vQuestionnaireQuestions.FirstOrDefault(q =&gt; q.Questionnaire_ID == id); } public IEnumerable&lt;Multiple_Choice_Answers&gt; FindAllMultipleChoiceAnswersTest() { return db.Multiple_Choice_Answers; } public Multiple_Choice_Answers GetMultipleChoiceAnswersTest(int id) { return db.Multiple_Choice_Answers.FirstOrDefault(q =&gt; q.Question_ID == id); } </code></pre> <p>and added it to the inherits of my view:</p> <pre><code>&lt;%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage&lt;IEnumerable&lt;QuestionnaireApp.Models.Questionnaire&gt;&gt;" %&gt; </code></pre> <p>The model information does not seem to be making it as now all my item.fieldname's are coming back as not having a definition. Am I over complicating this?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload