Posting Textarea to the controller (currently passing null) - MVC

I create a website where the user fills in a text box (in response to a question), and then click on. When they click next, the text box will be sent to the controller and the next question will be restored (I will also save the answer in a cookie). Unfortunately, when I run the code in debugging, I realized that the text field is not sent b / c, the parameter is null. I tried to figure it out, and I looked around, and it looks like I'm doing it right. Hope this is easy to fix. Thanks so much for your time!

Controller:

// POST: /Question/1 [HttpPost] public ActionResult q(string textAnswer) { if (textAnswer != null) ViewBag.current++; Question q = db.Questions.Find(ViewBag.current); if (q == null) { return RedirectToAction("submit"); } return View(q); } 

View:

 <form class="form-horizontal" id="myForm" method="post" enctype="text/plain" name="input"> <p> <h3>Question <span id="integer">@ViewBag.current</span></h3> @Html.DisplayFor(model => model.Ques) </p> <div class="control-group"> <label class="control-label">Answer</label> <div class="controls"> <textarea rows="20" style="width:600px" id="textAnswer"></textarea> </div> </div> <div class="control-group"> <div class="controls" > <button onclick="history.back()" type="button" class="btn">Back</button> <input type="submit" class="btn" value="Next" /> </div> </div> </form> 
+7
source share
2 answers

Your text field should have a name for the publication.

 <textarea rows="20" style="width:600px" id="textAnswer" name="textAnswer"></textarea> 

Also remove the enctype attribute from your form.

+9
source

Text areas do not put strings; they send string arrays .
The controller must have this signature:

 public ActionResult q(string[] textAnswer) 
+1
source

All Articles