1

Right now I am trying to generate textareas to provide responses to a variable number of questions. I have finished the question side of things. This is my current solution for the responses side of things inside the cshtml I have.

          @foreach (var question in ViewBag.Questions)
          {                         
            <div class="form-group">
                <label asp-for="ResponseToList" class="control-label"> @question.QuestionText</label>
                <textarea asp-for="ResponseToList" class="form-control" rows="3" required></textarea>
                <span asp-validation-for="ResponseToList" class="text-danger"></span>
            </div>
          }

This creates the correct number of boxes with the correct questions as lables. However when I submit the form no matter how many textareas are generated only the first response is added to the array. Here is the code in the view-model

    public List<string> Responses = new List<string>();
    public string ResponseToList
    {
        get
        {
            return "Test";
        }
        set
        {
            Responses.Add(value.ToString());
        }
    }

Any help would be much appreciated

  • 1
    https://stackoverflow.com/questions/48833363/asp-net-mvc-send-listfoo-collection-from-my-view-to-a-controller – Chetan Nov 08 '19 at 00:50
  • 1
    you seem to be taking an unnecessary complicated route here, consider just adding all the values of these text to a hidden input onsubmit. – Brett Caswell Nov 08 '19 at 01:00

0 Answers0