hope everything is good. I recently approached C# and I'm beginner, so I can't get out from this problem. I'm trying send some data from view to controller, but everything I tried didn't work and variables were always empty. In particular, I have a loop that creates some textarea fields and I want to send to controller the values inserted by user (in those textarea fields) when submitting. But my string array doesn't work as I expected. Hope I didn't make this stuff too bad. Here is the code:
Quest.cshtml
@{
WebDiz2.Models.FileTXTMethodsModel FTMM = new WebDiz2.Models.FileTXTMethodsModel();
List<FileTXTModel> quest = FTMM.Function2(ViewBag.quest_type, ViewBag.quest_number);
}
<form name="RequestingParam2" asp-area="" asp-controller="Home" asp-action="Result" method="POST">
@{
string[] answers = new string[quest.Count];
int index = 0;
for (var i = 0; i < quest.Count; i++)
{
answers[i] = "contentn2" + index;
<table>
@quest[i].ParolaRicercata <br>
<textarea name=@answers[i] class="textareaquest" maxlength="30" placeholder="Risposta" required> </textarea> <br>
</table>
++index;
}
}
<input type="hidden" name="loop_length" [email protected]>
<br> <button type="submit" class="checkandinvio"> INVIO </button> <br> <br>
</form>
HomeController.cs
[HttpPost]
public IActionResult Result(string[] answers, int loop_length)
{
string[] support1 = new string[loop_length];
for (int i=0; i<loop_length; ++i)
{
support1[i] = answers[i];
}
ViewBag.support2 = support1;
ViewBag.ll = loop_length;
return View();
}
I tried some ways to match textarea name with controller's parameters, but understood that 'string[] answers' in public IActionResult can't get loop values I submitted; but I really don't know how to make it able to understand I'm passing multiple values or how to store that values in the way for being recognized by controller. Thanks in advance for your patience and your time, guys!