Hi i am developing an application in MVC using nhibibernate.
It is an Question-Answer Forum where on the first Page a list of questions are displayed as link.Now when i click on the link it moves to the Answers Page where the answers are displayed for the particular Question.
I have two tables with the following fields
Question Table:
QuestionID int
Question nvarchar(255)
Created_Date datetime
Modified_Date datetime
Created_By int
Modified_By int
Deleted nchar(1)
Answers Table:
AnswerId int
Answer nvarchar(255)
Created_Date datetime
Modified_Date datetime
Created_By int
Modified_By int
QuestionID int (foreign key)
Deleted nchar(1)
This is what i have tried:
Inside QuestionPage.cshtml
@using (Html.BeginForm("Question_Page","QuestionAnswer",FormMethod.Post))
{
@Html.ValidationSummary(true)
<ul>
@foreach (var item in Model)
{
<li>@Html.ActionLink(item.Question, "Answer_Page", new { Id = item.QuestionID, Question = item.Question })</li>
}
</ul>
<br />
<h2><label for="PostyourQuestion:">Post your Question:</label></h2>
@Html.TextArea("PostyourQuestion")
<br /><br />
<input type="submit"/>
}
Inside the Controller:
public ActionResult Answer_Page(int Id, string Question)
{
var model = new AnswerService().GetAllAnswers();
ViewBag.Question = Question;
model = (from x in model
where x.QuestionID.Equals(Id)
select x).ToList();
return View(model);
}
[HttpPost]
public ActionResult Answer_Page(Answers ans, string PostyourAnswer, int Id,string Question)
{
ViewBag.Question =Question;
ans.Answer = PostyourAnswer;
ans.CreatedDate = DateTime.Now;
ans.ModifiedDate = DateTime.Now;
ans.CreatedBy = 101;
ans.ModifiedBy = 101;
ans.FkQuestionID= Id;
if (ModelState.IsValid)
{
new AnswerService().SaveOrUpdateAnswers(ans);
var model = new AnswerService().GetAllAnswers();
model = (from x in model
where x.QuestionID.Equals(Id)
select x).ToList();
return View(model);
}
return View();
}
But the problem is OnPost i get a null value inside the Question variable and hence i am not able to display the question on post when the user submits the form.
Please help me.I know i am missing sumthing silly..
try changing to
Now you should be able to get it in the Question variable.
Personally i don’t think it is a good idea to pass the question as it is in the query string. Query string has a limit of characters i believe. you should ideally pass the question id only and build the question object again in your action method by querying your persistent data source.
What is the maximum possible length of a query string?
http://classicasp.aspfaq.com/forms/what-is-the-limit-on-querystring/get/url-parameters.html