Note that there are some explanatory texts on larger screens.

plurals
  1. POAggregate all my textbox values into a list before posting the form
    text
    copied!<p>I am building an MVC form with a bunch of textboxes. However, my model requires a list of strings. What I'm trying to do is have it so when the user hits submit, before the form posts, it takes all of the values in the text box, adds it to a List and appends it to the model for submitting. I'm stuck at how I would be able to do this?</p> <p>The reason I need to do this is that I need the form to be dynamic in that the # of fields can grow and the page adapts to that. I can't hardcode the individual fields into the model as the model would need to adapt to the length of the form.</p> <p>Here's my code so far:</p> <p>The HTML</p> <pre><code>@using (Html.BeginForm(new { id = "feedbackForm" })) { &lt;fieldset&gt; @foreach (Feedback_Num_Questions questionForm in FeedbackSurveyQuestions) { string textBoxID = "feedbackq"; questionNumberTextBoxes++; textBoxID = textBoxID + questionNumberTextBoxes.ToString(); &lt;input type="text" id="@textBoxID" /&gt; } &lt;input type="text" id="txtAdditionalComments" /&gt; &lt;input type="submit" value="Create" name="btnFeedbackSubmit" id="btnSubmitFeedbackForm" /&gt; &lt;/fieldset&gt; } </code></pre> <p>The Model:</p> <pre><code> public class PostCourseFeedbackModel { public List&lt;string&gt; responseList { get; set; } public decimal pelID { get; set; } public string employeeid { get; set; } } </code></pre> <p>Thanks for the help!</p> <p>UPDATE: after getting a response from FLEM, I revised my code a bit to this. Still not quite working, but was hoping somebody could spot out the issue:</p> <p>HTML:</p> <pre><code> @using (Html.BeginForm(new { id = "feedbackForm" })) { &lt;fieldset&gt; @{ int i = 0; foreach (Feedback_Num_Questions questionForm in FeedbackSurveyQuestions) { i++; string textBoxID = "feedbackq" + i.ToString(); @Html.TextBoxFor(m =&gt; m.responseList[i], new { @id = textBoxID }); } &lt;input type="submit" value="Create" name="btnFeedbackSubmit" id="btnSubmitFeedbackForm" /&gt; } &lt;/fieldset&gt; } </code></pre> <p>The model remained unchanged...</p> <p>UPDATE - As requested by flem, I am posting my action. However this is a barebones action as I have yet to code what I plan to do with the data...</p> <pre><code> [HttpPost] public ActionResult PostCourseFeedback(PostCourseFeedbackModel feedback) { return RedirectToAction("FeedbackSubmitted", "Feedback", new { status = "success" }); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload