I am creating a forum application in mvc3…
I have a link called Add New Comment on Post page where user can add the comments for that post… for creating new comment i have written following code….
public ActionResult Addnew(int id)
{
Answer ans = new Answer();
ans.QuestionQId = id;
return View();
}
[HttpPost]
public ActionResult Addnew(Answer ans,int id)
{
_db.Answers.Add(ans);
_db.SaveChanges();
return View("Details");
}
but it is giving mean error whenever i try to save the code as follows:
Value cannot be null.
Parameter name: entity
I have two different table Question{id(pk), Question} and Answer{id(pk), ans, Qid(fk)}
All i want to do is While adding the comment for a perticular question, its Qid will be stored in answer database …..
Help me!!
View Related —
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<fieldset>
<legend>Answer</legend>
<div class="editor-label">
@Html.LabelFor(model => model.AId)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.AId)
@Html.ValidationMessageFor(model => model.AId)
</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>
<div class="editor-label">
@Html.LabelFor(model => model.QuestionQId)
</div> <div class="display-field">
@Html.DisplayFor(modelItem => modelItem.QuestionQId)
</div>
}
I think you are not passing the Answer object to the view, may caused the problem. have you tried following