The common programming pattern is taking advantage of model binding where the input name (html) contains an index.
@page
@model RazorPagesDemo.Pages.BindCollectionExModel
@{
ViewData["Title"] = "Model Bind a Collection";
}
<div>
<form method="post">
@for (int i = 0; i < Model.MyArray.Length; i++)
{
<input type="hidden" asp-for="MyArray[i]" />
}
<input type="submit" value="submit" />
</form>
</div>
<div>
<ul>
@foreach (int item in Model.SubmittedValues)
{
<li>
@item
</li>
}
</ul>
</div>
public class BindCollectionExModel : PageModel
{
public void OnGet()
{
//Populate myArray
MyArray = new int[] { 1, 2, 3, 4, 5 };
}
public void OnPost()
{
SubmittedValues.AddRange(MyArray);
}
[BindProperty]
public int[] MyArray { get; set; }
public List<int> SubmittedValues { get; set; } = new List<int>();
}