Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Posting Textarea to Controller (currently null is being passed) - MVC

I'm creating a website where the user will fill in a textarea (in response to a question) and then press next. When they press next, the textarea will be submitted to the controller and the next question will be retrieved (I'll also store the answer in a cookie). Unfortunately, when I run the code in debug I've realized that the textarea is not being submitted b/c the parameter is null. I've tried to figure it out and I've looked around and I seem to be doing it properly. Hopefully it's an easy 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>
like image 327
Dehli Avatar asked May 22 '13 20:05

Dehli


2 Answers

Your textarea needs a name in order to be posted.

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

Also remove the enctype attribute from your form.

like image 187
km6zla Avatar answered Nov 10 '22 18:11

km6zla


Text areas don't post strings, they post string arrays.
Your controller should have this signature:

public ActionResult q(string[] textAnswer)
like image 2
pablo ferrari Avatar answered Nov 10 '22 19:11

pablo ferrari