EqEdi
EqEdi

Reputation: 61

How to show data from two different tables in one 1 view

I'm creating a forum where user can ask questions and comment on it ....using mvc3

Inside that i have a link "details" for each question .... in side that i have 2 more links "show" and "add comments" in show one can see all comments related to a particular Question

and in Add comments we can add a comment for particular question its controller code is as follow:

   public ActionResult Addnew(int id)
    {
              return View();
    }
    [HttpPost]
    public ActionResult Addnew(Answer answr, int id)
    {
        answr.QuestionQId = id;
        _db.Answers.Add(answr);
        _db.SaveChanges();
        return RedirectToAction("Index");
    }

and in view i have written:

@model Hobby.Models.Answer

@{
  ViewBag.Title = "Addnew";
}

<h2>Addnew</h2>

    <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

        @using (Html.BeginForm()) {
         @Html.ValidationSummary(true)
             <fieldset>
             <legend>Answer</legend>
            <div class="display-label">Question</div>
             <div class="display-field">
           @Html.DisplayFor(model => model.Question.Que)
         </div>

             <div class="editor-label">
                @Html.LabelFor(model => model.Ans)
            </div>
            <div class="editor-field">
                  @Html.EditorFor(model => model.Ans)
                    @Html.ValidationMessageFor(model => model.Ans)
            </div>
              <p>
                 <input type="submit" value="Add Comment" />
               </p>
            </fieldset>
        }
  <div>
   @Html.ActionLink("Back to List", "Index")
 </div>

but it is not showing the Question from question table..... Please help me

Upvotes: 1

Views: 1461

Answers (2)

Mauricio Morales
Mauricio Morales

Reputation: 1018

@Jayanga is right but I think you still wanted to show data from "2 tables" as you stated in the title of your question. Assuming you mean that you want to show the question and all related answers, and also assuming that this is .NET Entity Framework (don't know by heart the syntax of Linq2Sql), you would do:

public ActionResult Addnew(int id)
{
    var question = _db.Questions.Include("Answers").SingleOrDefault(q => q.Id == id);
    return View(question);
}

This changes your binding model type to Question, but I'm assuming here that a Question can have more than 1 answer, and in that case, you'll have in your View all the information you need... something like this:

// In your .cshtml file
<article class="question">@Model.Que</article> @* this is your question text *@
@foreach (var answer in Model.Answers.OrderBy(a => a.PostedDate)) { @* assuming you want to sort this *@
    <article class="answer">@answer.Ans</article> @* this would be your answer text *@
}

Upvotes: 0

Jayanga
Jayanga

Reputation: 887

I don't see where you are binding the necessary data I think AddNew [Get] method should get the question from DB and set it to the view, I suppose you should do something similar to this

public ActionResult Addnew(int id)
    {
              Answer ans = new Answer();
              ans.Question = _db.Questions.SingleOrDefault(q => q.Id == id);
              return View(ans);
    }

Upvotes: 1

Related Questions