Hi @Adrian N ,
To pass a List of objects to the controller, you could use the following methods:
- Use asp.net core Model Binding and Submit button to submit the form. For example: submit list of questions to the controller: QuestionIndex.cshtml (you could change the action name, object name or request url to yours)
Controller:@model List<WebApplication6.Models.Question> <form asp-action="QuestionIndex" id="mainform" method="POST"> @for (var i = 0; i < Model.Count(); i++) { <div>@Model[i].QuestionTitle</div> <input asp-for="@Model[i].QuestionId" type="hidden" /> <input asp-for="@Model[i].QuestionTitle" type="hidden" /> <input asp-for="@Model[i].QuestionAnswer" /> <br /> } <input type="submit" class="btn btn-primary" value="Submit"> </form>
[HttpPost] public IActionResult QuestionIndex(IEnumerable<Question> questions) { //do something return View(_context.Questions.ToList()); }
- Using JQuery Ajax submits the form data or an array of JavaScript objects to the controller. For example: add a button in the above index page, then, in the button click event, use Ajax method to submit the form to the controller.
Comment: Here I use a screenshot to show the code, because I tried to post the sample code, but it disappears when preview.
The screenshot like this:
More detail information, you could refer my reply in this thread.
------
If the answer doesn’t solve your issue, please provide more details of error that will help us track down what’s happening.
If the answer is helpful, please click "Accept Answer" and vote it up.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best Regards,
Dillion