0

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

user1274646
  • 921
  • 6
  • 21
  • 46

2 Answers2

0

try changing to

@Html.ActionLink(item.Question, "Answer_Page","QuestionAnswer", new { @Id = item.QuestionID, @Question = item.Question },null)

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

Community
  • 1
  • 1
Shyju
  • 214,206
  • 104
  • 411
  • 497
  • Hi,thanx 4 d reply..but i am act new to mvc can u show me how to fire a query in the controller in order to fetch only the value equal to the Id value – user1274646 Mar 25 '12 at 13:28
  • hey buddy thanx 4 the idea..i got it..i fired a query inside the controller and m able to fetch the value. – user1274646 Mar 25 '12 at 13:45
  • @user1274646 i dont know anything abt your tables. try to copy the query which gives you all questions in the main page and add a where condition for the question id (where QuestionId=id) – Shyju Mar 25 '12 at 13:47
0

There are a couple of ways to handle this. The best way, IMO, is to encode the question id into the url and go get the question from your repository again. Implement caching in your repository, or as a layer that wraps your repository, if more performance is needed. The other way is to use TempData (essentially the Session) to hold the data over one request cycle. This method has some failure modes - they click another action on the page then use their back button, for example - but is also relatively simple. You don't want to use the ViewBag. The ViewBag is intended to pass ancillary data to the view as a one-way transfer. As you've found, it doesn't retain its value over a request cycle.

FWIW, I think you should be using the question id in both methods, not passing the question itself around as part of the query string.

tvanfosson
  • 524,688
  • 99
  • 697
  • 795