What is the ASP.NET equivalent way of doing this in the Controller method?
I recommend taking advantage of MVC model binding.
https://learn.microsoft.com/en-us/aspnet/core/mvc/models/model-binding?view=aspnetcore-7.0
The controller action simply defines a C# type that matches the post parameters. In the example below the markup has three inputs named Options.
<h2>Index</h2>
<form method="post">
<input type="hidden" name="Options" value="Closed Door" />
<input type="hidden" name="Options" value="Power Restored" />
<input type="hidden" name="Options" value="Support Call Raised" />
<input type="submit" value="submit" />
</form>
The browser sends a list of options in the HTTP body
Options=Closed+Door&Options=Power+Restored&Options=Support+Call+Raised
The controller action just needs a parameter name that matches the input name which is "Options" and the type is an array or a collection.
// GET: ModelBindingEx
[HttpGet]
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(List<string> Options)
{
return Json(Options);
}
You could also have a complex type where the post payload is an array and anther value. In this case you wrap the properties in a class.
<form method="post">
<input type="hidden" name="Id" value="1" />
<input type="hidden" name="Options" value="Closed Door" />
<input type="hidden" name="Options" value="Power Restored" />
<input type="hidden" name="Options" value="Support Call Raised" />
<input type="submit" value="submit" />
</form>
public class MyViewModel
{
public int Id { get; set; }
public List<string> Options { get; set; }
}
[HttpGet]
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(MyViewModel model)
{
return Json(model);
}
Lastly, you can also access the parameters using the Request collection.
[HttpPost]
public ActionResult Index(MyViewModel model)
{
string[] items = ((string)Request["Options"]).Split(',');
return Json(items);
}