How am I able to select multiple items in a Listbox that will be passed to the controller's Post model
parameter?
I'm able to physically select multiple via holding CTRL, but when I submit, I get the validation error message, "The field TagId must be a number." It only submits with one item selected.
Create View Form showing multiple items selected
The List Box
@Html.ListBoxFor(model => model.allTags[0].TagId, new SelectList(Model.allTags, "TagId", "Name"), new { @class = "form-control", @Id = "SelectTags", @style = "width:200px;height:300px;" })
The controller Post method
[HttpPost]
public ActionResult Create(CreateRecipe model)
{
try
{
// TODO: Add insert logic here
return RedirectToAction("ViewRecipes");
}
catch
{
return View();
}
}
Thank you
You can try the following code to select multiple items from ListBoxFor and pass it to controller Post method.
Controller:
Model:
Index.cshtml:
Result: