Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can a single view access multiple models in mvc3
    primarykey
    data
    text
    <p>i am developing an application in MVC3. Basically it is an Question Forum where on Question page user will see all questions as link and a post comment box at the end.and when he clicks on question link he is moved to the answers page.now the answer page is using another model class and their i am not able to acess my answer class data</p> <p>what i found was every Controller has a view and a model but i want my view to access multiple models and i dont knw how to go about it..</p> <p>I tried this:</p> <pre><code> public class MainClass { public Question_Page question{ get; set; } public Answer_Page answer { get; set; } } public class Question_Page { public virtual int Id { get; set; } public virtual string Question_name { get; set; } public virtual DateTime Created_Date { get; set; } public virtual DateTime Modified_Date { get; set; } public virtual int Created_By { get; set; } public virtual int Modified_By { get; set; } public virtual char Deleted { get; set; } public Question_Page() { this.Deleted='F'; } } public class Answer_Page { public virtual int Id { get; set; } public virtual string Answer { get; set; } public virtual DateTime Created_Date { get; set; } public virtual DateTime Modified_Date { get; set; } public virtual int Created_By { get; set; } public virtual int Modified_By { get; set; } public virtual char Deleted { get; set; } public virtual Question_Page Question { get; set; } public Answer_Page() { this.Deleted='F'; } } </code></pre> <p>Now inside the view of Questions List where i display a list of questions:</p> <pre><code>@model IEnumerable&lt;Core.Model.MainPage&gt; @{ ViewBag.Title = "Index"; } &lt;style type="text/css"&gt; ul { list-style-type: none; } &lt;/style&gt; &lt;h2&gt;All Questions&lt;/h2&gt; &lt;hr /&gt; @using (Html.BeginForm("Index","Question",FormMethod.Post)) { @Html.ValidationSummary(true) &lt;ul&gt; @foreach (var item in Model) { &lt;li&gt;@Html.ActionLink(item.Question_name, "answer", new { Qry = item.Id })&lt;/li&gt; } &lt;/ul&gt; &lt;label for="PostyourQuestion:"&gt;Post your Question:&lt;/label&gt;&lt;br /&gt;&lt;br /&gt; @Html.TextArea("textID") &lt;br /&gt; &lt;input type="submit"/&gt; } </code></pre> <p>after doing this much i am gettin errorin line: @foreach (var item in Model) </p> <p>This is my controller:</p> <pre><code> public ActionResult Index() { return View(new StudentService().GetAllStudents()); } [HttpPost] public ActionResult Index(Question_Page question,string textID) { question.Question_name = textID; question.Created_Date = DateTime.Now; question.Modified_Date = DateTime.Now; question.Created_By = 101; question.Modified_By = 101; new StudentService().SaveOrUpdateStudent(question); return View(new StudentService().GetAllStudents()); } </code></pre> <p>StudentService is a class where i hv defined the methods GetAllStudents(),SaveOrUpdateStudent()</p> <p>Please help me</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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