In ASP.NET Core, I have searched around but cannot figure out how to pass a list from the view to controller. I can create a form and pass a single int value, but not a List<int>.Here is what I have to pass a single value
<form id="myForm">
<div class="modal-body">
<div class="form-group">
<label class="custom-label">
<div class="col-sm-3">
<input name="singleValue" type="text" class="form-control" />
</div>
</div>
<a href="#" class="btn btn-success" onclick="SaveForm('UpdateFunction', 'ControllerName')">Update</a>
</form>
My controller is
public IActionResult UpdateFunction(int singleValue)
{
var model = _context.TableName.SingleOrDefault(f => f.Id == singleValue);
model.Name = "Whatever";
_context.Update(model);
_context.SaveChanges();
return Json(true);
}
However because I would like to update multiple values at a time instead of just int singleValue, I have tried passing
public IActionResult UpdateFunction(int[] list) or
public IActionResult UpdateFunction(List<int> list)
into the function parameters. I have tried entering multiple int values into the text box with/without commas, brackets, nothing works and the lists are always empty. My goal is to just pass the list or array to controller, then loop through the values in the list. My question is how can I pass a List<int> to the controller?