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
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